Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: fusl/ldso/dynlink.c

Issue 1714623002: [fusl] clang-format fusl (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: headers too Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #define _GNU_SOURCE 1 #define _GNU_SOURCE
2 #include <stdio.h> 2 #include <stdio.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <stdarg.h> 4 #include <stdarg.h>
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <string.h> 6 #include <string.h>
7 #include <unistd.h> 7 #include <unistd.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <elf.h> 9 #include <elf.h>
10 #include <sys/mman.h> 10 #include <sys/mman.h>
11 #include <limits.h> 11 #include <limits.h>
12 #include <fcntl.h> 12 #include <fcntl.h>
13 #include <sys/stat.h> 13 #include <sys/stat.h>
14 #include <errno.h> 14 #include <errno.h>
15 #include <link.h> 15 #include <link.h>
16 #include <setjmp.h> 16 #include <setjmp.h>
17 #include <pthread.h> 17 #include <pthread.h>
18 #include <ctype.h> 18 #include <ctype.h>
19 #include <dlfcn.h> 19 #include <dlfcn.h>
20 #include "pthread_impl.h" 20 #include "pthread_impl.h"
21 #include "libc.h" 21 #include "libc.h"
22 #include "dynlink.h" 22 #include "dynlink.h"
23 23
24 static void error(const char *, ...); 24 static void error(const char*, ...);
25 25
26 #define MAXP2(a,b) (-(-(a)&-(b))) 26 #define MAXP2(a, b) (-(-(a) & -(b)))
27 #define ALIGN(x,y) ((x)+(y)-1 & -(y)) 27 #define ALIGN(x, y) ((x) + (y)-1 & -(y))
28 28
29 struct debug { 29 struct debug {
30 » int ver; 30 int ver;
31 » void *head; 31 void* head;
32 » void (*bp)(void); 32 void (*bp)(void);
33 » int state; 33 int state;
34 » void *base; 34 void* base;
35 }; 35 };
36 36
37 struct td_index { 37 struct td_index {
38 » size_t args[2]; 38 size_t args[2];
39 » struct td_index *next; 39 struct td_index* next;
40 }; 40 };
41 41
42 struct dso { 42 struct dso {
43 #if DL_FDPIC 43 #if DL_FDPIC
44 » struct fdpic_loadmap *loadmap; 44 struct fdpic_loadmap* loadmap;
45 #else 45 #else
46 » unsigned char *base; 46 unsigned char* base;
47 #endif 47 #endif
48 » char *name; 48 char* name;
49 » size_t *dynv; 49 size_t* dynv;
50 » struct dso *next, *prev; 50 struct dso *next, *prev;
51 51
52 » Phdr *phdr; 52 Phdr* phdr;
53 » int phnum; 53 int phnum;
54 » size_t phentsize; 54 size_t phentsize;
55 » int refcnt; 55 int refcnt;
56 » Sym *syms; 56 Sym* syms;
57 » uint32_t *hashtab; 57 uint32_t* hashtab;
58 » uint32_t *ghashtab; 58 uint32_t* ghashtab;
59 » int16_t *versym; 59 int16_t* versym;
60 » char *strings; 60 char* strings;
61 » unsigned char *map; 61 unsigned char* map;
62 » size_t map_len; 62 size_t map_len;
63 » dev_t dev; 63 dev_t dev;
64 » ino_t ino; 64 ino_t ino;
65 » signed char global; 65 signed char global;
66 » char relocated; 66 char relocated;
67 » char constructed; 67 char constructed;
68 » char kernel_mapped; 68 char kernel_mapped;
69 » struct dso **deps, *needed_by; 69 struct dso **deps, *needed_by;
70 » char *rpath_orig, *rpath; 70 char *rpath_orig, *rpath;
71 » struct tls_module tls; 71 struct tls_module tls;
72 » size_t tls_id; 72 size_t tls_id;
73 » size_t relro_start, relro_end; 73 size_t relro_start, relro_end;
74 » void **new_dtv; 74 void** new_dtv;
75 » unsigned char *new_tls; 75 unsigned char* new_tls;
76 » volatile int new_dtv_idx, new_tls_idx; 76 volatile int new_dtv_idx, new_tls_idx;
77 » struct td_index *td_index; 77 struct td_index* td_index;
78 » struct dso *fini_next; 78 struct dso* fini_next;
79 » char *shortname; 79 char* shortname;
80 #if DL_FDPIC 80 #if DL_FDPIC
81 » unsigned char *base; 81 unsigned char* base;
82 #else 82 #else
83 » struct fdpic_loadmap *loadmap; 83 struct fdpic_loadmap* loadmap;
84 #endif 84 #endif
85 » struct funcdesc { 85 struct funcdesc {
86 » » void *addr; 86 void* addr;
87 » » size_t *got; 87 size_t* got;
88 » } *funcdescs; 88 } * funcdescs;
89 » size_t *got; 89 size_t* got;
90 » char buf[]; 90 char buf[];
91 }; 91 };
92 92
93 struct symdef { 93 struct symdef {
94 » Sym *sym; 94 Sym* sym;
95 » struct dso *dso; 95 struct dso* dso;
96 }; 96 };
97 97
98 int __init_tp(void *); 98 int __init_tp(void*);
99 void __init_libc(char **, char *); 99 void __init_libc(char**, char*);
100 void *__copy_tls(unsigned char *); 100 void* __copy_tls(unsigned char*);
101 101
102 __attribute__((__visibility__("hidden"))) 102 __attribute__((__visibility__("hidden"))) const char* __libc_get_version(void);
103 const char *__libc_get_version(void);
104 103
105 static struct builtin_tls { 104 static struct builtin_tls {
106 » char c; 105 char c;
107 » struct pthread pt; 106 struct pthread pt;
108 » void *space[16]; 107 void* space[16];
109 } builtin_tls[1]; 108 } builtin_tls[1];
110 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt) 109 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
111 110
112 #define ADDEND_LIMIT 4096 111 #define ADDEND_LIMIT 4096
113 static size_t *saved_addends, *apply_addends_to; 112 static size_t *saved_addends, *apply_addends_to;
114 113
115 static struct dso ldso; 114 static struct dso ldso;
116 static struct dso *head, *tail, *fini_head; 115 static struct dso *head, *tail, *fini_head;
117 static char *env_path, *sys_path; 116 static char *env_path, *sys_path;
118 static unsigned long long gencnt; 117 static unsigned long long gencnt;
119 static int runtime; 118 static int runtime;
120 static int ldd_mode; 119 static int ldd_mode;
121 static int ldso_fail; 120 static int ldso_fail;
122 static int noload; 121 static int noload;
123 static jmp_buf *rtld_fail; 122 static jmp_buf* rtld_fail;
124 static pthread_rwlock_t lock; 123 static pthread_rwlock_t lock;
125 static struct debug debug; 124 static struct debug debug;
126 static struct tls_module *tls_tail; 125 static struct tls_module* tls_tail;
127 static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN; 126 static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN;
128 static size_t static_tls_cnt; 127 static size_t static_tls_cnt;
129 static pthread_mutex_t init_fini_lock = { ._m_type = PTHREAD_MUTEX_RECURSIVE }; 128 static pthread_mutex_t init_fini_lock = {._m_type = PTHREAD_MUTEX_RECURSIVE};
130 static struct fdpic_loadmap *app_loadmap; 129 static struct fdpic_loadmap* app_loadmap;
131 static struct fdpic_dummy_loadmap app_dummy_loadmap; 130 static struct fdpic_dummy_loadmap app_dummy_loadmap;
132 131
133 struct debug *_dl_debug_addr = &debug; 132 struct debug* _dl_debug_addr = &debug;
134 133
135 __attribute__((__visibility__("hidden"))) 134 __attribute__((__visibility__("hidden"))) void (*const __init_array_start)(
136 void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0; 135 void) = 0,
137 136 (*const __fini_array_start)(void) = 0;
138 __attribute__((__visibility__("hidden"))) 137
139 extern void (*const __init_array_end)(void), (*const __fini_array_end)(void); 138 __attribute__((__visibility__("hidden"))) extern void (*const __init_array_end)(
139 void),
140 (*const __fini_array_end)(void);
140 141
141 weak_alias(__init_array_start, __init_array_end); 142 weak_alias(__init_array_start, __init_array_end);
142 weak_alias(__fini_array_start, __fini_array_end); 143 weak_alias(__fini_array_start, __fini_array_end);
143 144
144 static int dl_strcmp(const char *l, const char *r) 145 static int dl_strcmp(const char* l, const char* r) {
145 { 146 for (; *l == *r && *l; l++, r++)
146 » for (; *l==*r && *l; l++, r++); 147 ;
147 » return *(unsigned char *)l - *(unsigned char *)r; 148 return *(unsigned char*)l - *(unsigned char*)r;
148 } 149 }
149 #define strcmp(l,r) dl_strcmp(l,r) 150 #define strcmp(l, r) dl_strcmp(l, r)
150 151
151 /* Compute load address for a virtual address in a given dso. */ 152 /* Compute load address for a virtual address in a given dso. */
152 #if DL_FDPIC 153 #if DL_FDPIC
153 static void *laddr(const struct dso *p, size_t v) 154 static void* laddr(const struct dso* p, size_t v) {
154 { 155 size_t j = 0;
155 » size_t j=0; 156 if (!p->loadmap)
156 » if (!p->loadmap) return p->base + v; 157 return p->base + v;
157 » for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++); 158 for (j = 0; v - p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz;
158 » return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].ad dr); 159 j++)
159 } 160 ;
160 #define fpaddr(p, v) ((void (*)())&(struct funcdesc){ \ 161 return (void*)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
161 » laddr(p, v), (p)->got }) 162 }
163 #define fpaddr(p, v) ((void (*)()) & (struct funcdesc){laddr(p, v), (p)->got})
162 #else 164 #else
163 #define laddr(p, v) (void *)((p)->base + (v)) 165 #define laddr(p, v) (void*)((p)->base + (v))
164 #define fpaddr(p, v) ((void (*)())laddr(p, v)) 166 #define fpaddr(p, v) ((void (*)())laddr(p, v))
165 #endif 167 #endif
166 168
167 static void decode_vec(size_t *v, size_t *a, size_t cnt) 169 static void decode_vec(size_t* v, size_t* a, size_t cnt) {
168 { 170 size_t i;
169 » size_t i; 171 for (i = 0; i < cnt; i++)
170 » for (i=0; i<cnt; i++) a[i] = 0; 172 a[i] = 0;
171 » for (; v[0]; v+=2) if (v[0]-1<cnt-1) { 173 for (; v[0]; v += 2)
172 » » a[0] |= 1UL<<v[0]; 174 if (v[0] - 1 < cnt - 1) {
173 » » a[v[0]] = v[1]; 175 a[0] |= 1UL << v[0];
174 » } 176 a[v[0]] = v[1];
175 } 177 }
176 178 }
177 static int search_vec(size_t *v, size_t *r, size_t key) 179
178 { 180 static int search_vec(size_t* v, size_t* r, size_t key) {
179 » for (; v[0]!=key; v+=2) 181 for (; v[0] != key; v += 2)
180 » » if (!v[0]) return 0; 182 if (!v[0])
181 » *r = v[1]; 183 return 0;
182 » return 1; 184 *r = v[1];
183 } 185 return 1;
184 186 }
185 static uint32_t sysv_hash(const char *s0) 187
186 { 188 static uint32_t sysv_hash(const char* s0) {
187 » const unsigned char *s = (void *)s0; 189 const unsigned char* s = (void*)s0;
188 » uint_fast32_t h = 0; 190 uint_fast32_t h = 0;
189 » while (*s) { 191 while (*s) {
190 » » h = 16*h + *s++; 192 h = 16 * h + *s++;
191 » » h ^= h>>24 & 0xf0; 193 h ^= h >> 24 & 0xf0;
192 » } 194 }
193 » return h & 0xfffffff; 195 return h & 0xfffffff;
194 } 196 }
195 197
196 static uint32_t gnu_hash(const char *s0) 198 static uint32_t gnu_hash(const char* s0) {
197 { 199 const unsigned char* s = (void*)s0;
198 » const unsigned char *s = (void *)s0; 200 uint_fast32_t h = 5381;
199 » uint_fast32_t h = 5381; 201 for (; *s; s++)
200 » for (; *s; s++) 202 h += h * 32 + *s;
201 » » h += h*32 + *s; 203 return h;
202 » return h; 204 }
203 } 205
204 206 static Sym* sysv_lookup(const char* s, uint32_t h, struct dso* dso) {
205 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso) 207 size_t i;
206 { 208 Sym* syms = dso->syms;
207 » size_t i; 209 uint32_t* hashtab = dso->hashtab;
208 » Sym *syms = dso->syms; 210 char* strings = dso->strings;
209 » uint32_t *hashtab = dso->hashtab; 211 for (i = hashtab[2 + h % hashtab[0]]; i; i = hashtab[2 + hashtab[0] + i]) {
210 » char *strings = dso->strings; 212 if ((!dso->versym || dso->versym[i] >= 0) &&
211 » for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { 213 (!strcmp(s, strings + syms[i].st_name)))
212 » » if ((!dso->versym || dso->versym[i] >= 0) 214 return syms + i;
213 » » && (!strcmp(s, strings+syms[i].st_name))) 215 }
214 » » » return syms+i; 216 return 0;
215 » } 217 }
216 » return 0; 218
217 } 219 static Sym* gnu_lookup(uint32_t h1,
218 220 uint32_t* hashtab,
219 static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const ch ar *s) 221 struct dso* dso,
220 { 222 const char* s) {
221 » uint32_t nbuckets = hashtab[0]; 223 uint32_t nbuckets = hashtab[0];
222 » uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4); 224 uint32_t* buckets = hashtab + 4 + hashtab[2] * (sizeof(size_t) / 4);
223 » uint32_t i = buckets[h1 % nbuckets]; 225 uint32_t i = buckets[h1 % nbuckets];
224 226
225 » if (!i) return 0; 227 if (!i)
226 228 return 0;
227 » uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]); 229
228 230 uint32_t* hashval = buckets + nbuckets + (i - hashtab[1]);
229 » for (h1 |= 1; ; i++) { 231
230 » » uint32_t h2 = *hashval++; 232 for (h1 |= 1;; i++) {
231 » » if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0) 233 uint32_t h2 = *hashval++;
232 » » && !strcmp(s, dso->strings + dso->syms[i].st_name)) 234 if ((h1 == (h2 | 1)) && (!dso->versym || dso->versym[i] >= 0) &&
233 » » » return dso->syms+i; 235 !strcmp(s, dso->strings + dso->syms[i].st_name))
234 » » if (h2 & 1) break; 236 return dso->syms + i;
235 » } 237 if (h2 & 1)
236 238 break;
237 » return 0; 239 }
238 } 240
239 241 return 0;
240 static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask) 242 }
241 { 243
242 » const size_t *bloomwords = (const void *)(hashtab+4); 244 static Sym* gnu_lookup_filtered(uint32_t h1,
243 » size_t f = bloomwords[fofs & (hashtab[2]-1)]; 245 uint32_t* hashtab,
244 » if (!(f & fmask)) return 0; 246 struct dso* dso,
245 247 const char* s,
246 » f >>= (h1 >> hashtab[3]) % (8 * sizeof f); 248 uint32_t fofs,
247 » if (!(f & 1)) return 0; 249 size_t fmask) {
248 250 const size_t* bloomwords = (const void*)(hashtab + 4);
249 » return gnu_lookup(h1, hashtab, dso, s); 251 size_t f = bloomwords[fofs & (hashtab[2] - 1)];
250 } 252 if (!(f & fmask))
251 253 return 0;
252 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS) 254
253 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE) 255 f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
256 if (!(f & 1))
257 return 0;
258
259 return gnu_lookup(h1, hashtab, dso, s);
260 }
261
262 #define OK_TYPES \
263 (1 << STT_NOTYPE | 1 << STT_OBJECT | 1 << STT_FUNC | 1 << STT_COMMON | \
264 1 << STT_TLS)
265 #define OK_BINDS (1 << STB_GLOBAL | 1 << STB_WEAK | 1 << STB_GNU_UNIQUE)
254 266
255 #ifndef ARCH_SYM_REJECT_UND 267 #ifndef ARCH_SYM_REJECT_UND
256 #define ARCH_SYM_REJECT_UND(s) 0 268 #define ARCH_SYM_REJECT_UND(s) 0
257 #endif 269 #endif
258 270
259 static struct symdef find_sym(struct dso *dso, const char *s, int need_def) 271 static struct symdef find_sym(struct dso* dso, const char* s, int need_def) {
260 { 272 uint32_t h = 0, gh, gho, *ght;
261 » uint32_t h = 0, gh, gho, *ght; 273 size_t ghm = 0;
262 » size_t ghm = 0; 274 struct symdef def = {0};
263 » struct symdef def = {0}; 275 for (; dso; dso = dso->next) {
264 » for (; dso; dso=dso->next) { 276 Sym* sym;
265 » » Sym *sym; 277 if (!dso->global)
266 » » if (!dso->global) continue; 278 continue;
267 » » if ((ght = dso->ghashtab)) { 279 if ((ght = dso->ghashtab)) {
268 » » » if (!ghm) { 280 if (!ghm) {
269 » » » » gh = gnu_hash(s); 281 gh = gnu_hash(s);
270 » » » » int maskbits = 8 * sizeof ghm; 282 int maskbits = 8 * sizeof ghm;
271 » » » » gho = gh / maskbits; 283 gho = gh / maskbits;
272 » » » » ghm = 1ul << gh % maskbits; 284 ghm = 1ul << gh % maskbits;
273 » » » } 285 }
274 » » » sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm); 286 sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
275 » » } else { 287 } else {
276 » » » if (!h) h = sysv_hash(s); 288 if (!h)
277 » » » sym = sysv_lookup(s, h, dso); 289 h = sysv_hash(s);
278 » » } 290 sym = sysv_lookup(s, h, dso);
279 » » if (!sym) continue; 291 }
280 » » if (!sym->st_shndx) 292 if (!sym)
281 » » » if (need_def || (sym->st_info&0xf) == STT_TLS 293 continue;
282 » » » || ARCH_SYM_REJECT_UND(sym)) 294 if (!sym->st_shndx)
283 » » » » continue; 295 if (need_def || (sym->st_info & 0xf) == STT_TLS ||
284 » » if (!sym->st_value) 296 ARCH_SYM_REJECT_UND(sym))
285 » » » if ((sym->st_info&0xf) != STT_TLS) 297 continue;
286 » » » » continue; 298 if (!sym->st_value)
287 » » if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue; 299 if ((sym->st_info & 0xf) != STT_TLS)
288 » » if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue; 300 continue;
289 301 if (!(1 << (sym->st_info & 0xf) & OK_TYPES))
290 » » if (def.sym && sym->st_info>>4 == STB_WEAK) continue; 302 continue;
291 » » def.sym = sym; 303 if (!(1 << (sym->st_info >> 4) & OK_BINDS))
292 » » def.dso = dso; 304 continue;
293 » » if (sym->st_info>>4 == STB_GLOBAL) break; 305
294 » } 306 if (def.sym && sym->st_info >> 4 == STB_WEAK)
295 » return def; 307 continue;
296 } 308 def.sym = sym;
297 309 def.dso = dso;
298 __attribute__((__visibility__("hidden"))) 310 if (sym->st_info >> 4 == STB_GLOBAL)
299 ptrdiff_t __tlsdesc_static(), __tlsdesc_dynamic(); 311 break;
300 312 }
301 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri de) 313 return def;
302 { 314 }
303 » unsigned char *base = dso->base; 315
304 » Sym *syms = dso->syms; 316 __attribute__((__visibility__("hidden"))) ptrdiff_t __tlsdesc_static(),
305 » char *strings = dso->strings; 317 __tlsdesc_dynamic();
306 » Sym *sym; 318
307 » const char *name; 319 static void do_relocs(struct dso* dso,
308 » void *ctx; 320 size_t* rel,
309 » int type; 321 size_t rel_size,
310 » int sym_index; 322 size_t stride) {
311 » struct symdef def; 323 unsigned char* base = dso->base;
312 » size_t *reloc_addr; 324 Sym* syms = dso->syms;
313 » size_t sym_val; 325 char* strings = dso->strings;
314 » size_t tls_val; 326 Sym* sym;
315 » size_t addend; 327 const char* name;
316 » int skip_relative = 0, reuse_addends = 0, save_slot = 0; 328 void* ctx;
317 329 int type;
318 » if (dso == &ldso) { 330 int sym_index;
319 » » /* Only ldso's REL table needs addend saving/reuse. */ 331 struct symdef def;
320 » » if (rel == apply_addends_to) 332 size_t* reloc_addr;
321 » » » reuse_addends = 1; 333 size_t sym_val;
322 » » skip_relative = 1; 334 size_t tls_val;
323 » } 335 size_t addend;
324 336 int skip_relative = 0, reuse_addends = 0, save_slot = 0;
325 » for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { 337
326 » » if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue; 338 if (dso == &ldso) {
327 » » type = R_TYPE(rel[1]); 339 /* Only ldso's REL table needs addend saving/reuse. */
328 » » if (type == REL_NONE) continue; 340 if (rel == apply_addends_to)
329 » » sym_index = R_SYM(rel[1]); 341 reuse_addends = 1;
330 » » reloc_addr = laddr(dso, rel[0]); 342 skip_relative = 1;
331 » » if (sym_index) { 343 }
332 » » » sym = syms + sym_index; 344
333 » » » name = strings + sym->st_name; 345 for (; rel_size; rel += stride, rel_size -= stride * sizeof(size_t)) {
334 » » » ctx = type==REL_COPY ? head->next : head; 346 if (skip_relative && IS_RELATIVE(rel[1], dso->syms))
335 » » » def = (sym->st_info&0xf) == STT_SECTION 347 continue;
336 » » » » ? (struct symdef){ .dso = dso, .sym = sym } 348 type = R_TYPE(rel[1]);
337 » » » » : find_sym(ctx, name, type==REL_PLT); 349 if (type == REL_NONE)
338 » » » if (!def.sym && (sym->st_shndx != SHN_UNDEF 350 continue;
339 » » » || sym->st_info>>4 != STB_WEAK)) { 351 sym_index = R_SYM(rel[1]);
340 » » » » error("Error relocating %s: %s: symbol not found ", 352 reloc_addr = laddr(dso, rel[0]);
341 » » » » » dso->name, name); 353 if (sym_index) {
342 » » » » if (runtime) longjmp(*rtld_fail, 1); 354 sym = syms + sym_index;
343 » » » » continue; 355 name = strings + sym->st_name;
344 » » » } 356 ctx = type == REL_COPY ? head->next : head;
345 » » } else { 357 def = (sym->st_info & 0xf) == STT_SECTION
346 » » » sym = 0; 358 ? (struct symdef){.dso = dso, .sym = sym}
347 » » » def.sym = 0; 359 : find_sym(ctx, name, type == REL_PLT);
348 » » » def.dso = dso; 360 if (!def.sym &&
349 » » } 361 (sym->st_shndx != SHN_UNDEF || sym->st_info >> 4 != STB_WEAK)) {
350 362 error("Error relocating %s: %s: symbol not found", dso->name, name);
351 » » if (stride > 2) { 363 if (runtime)
352 » » » addend = rel[2]; 364 longjmp(*rtld_fail, 1);
353 » » } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) { 365 continue;
354 » » » addend = 0; 366 }
355 » » } else if (reuse_addends) { 367 } else {
356 » » » /* Save original addend in stage 2 where the dso 368 sym = 0;
357 » » » * chain consists of just ldso; otherwise read back 369 def.sym = 0;
358 » » » * saved addend since the inline one was clobbered. */ 370 def.dso = dso;
359 » » » if (head==&ldso) 371 }
360 » » » » saved_addends[save_slot] = *reloc_addr; 372
361 » » » addend = saved_addends[save_slot++]; 373 if (stride > 2) {
362 » » } else { 374 addend = rel[2];
363 » » » addend = *reloc_addr; 375 } else if (type == REL_GOT || type == REL_PLT || type == REL_COPY) {
364 » » } 376 addend = 0;
365 377 } else if (reuse_addends) {
366 » » sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0; 378 /* Save original addend in stage 2 where the dso
367 » » tls_val = def.sym ? def.sym->st_value : 0; 379 * chain consists of just ldso; otherwise read back
368 380 * saved addend since the inline one was clobbered. */
369 » » switch(type) { 381 if (head == &ldso)
370 » » case REL_NONE: 382 saved_addends[save_slot] = *reloc_addr;
371 » » » break; 383 addend = saved_addends[save_slot++];
372 » » case REL_OFFSET: 384 } else {
373 » » » addend -= (size_t)reloc_addr; 385 addend = *reloc_addr;
374 » » case REL_SYMBOLIC: 386 }
375 » » case REL_GOT: 387
376 » » case REL_PLT: 388 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0;
377 » » » *reloc_addr = sym_val + addend; 389 tls_val = def.sym ? def.sym->st_value : 0;
378 » » » break; 390
379 » » case REL_RELATIVE: 391 switch (type) {
380 » » » *reloc_addr = (size_t)base + addend; 392 case REL_NONE:
381 » » » break; 393 break;
382 » » case REL_SYM_OR_REL: 394 case REL_OFFSET:
383 » » » if (sym) *reloc_addr = sym_val + addend; 395 addend -= (size_t)reloc_addr;
384 » » » else *reloc_addr = (size_t)base + addend; 396 case REL_SYMBOLIC:
385 » » » break; 397 case REL_GOT:
386 » » case REL_COPY: 398 case REL_PLT:
387 » » » memcpy(reloc_addr, (void *)sym_val, sym->st_size); 399 *reloc_addr = sym_val + addend;
388 » » » break; 400 break;
389 » » case REL_OFFSET32: 401 case REL_RELATIVE:
390 » » » *(uint32_t *)reloc_addr = sym_val + addend 402 *reloc_addr = (size_t)base + addend;
391 » » » » - (size_t)reloc_addr; 403 break;
392 » » » break; 404 case REL_SYM_OR_REL:
393 » » case REL_FUNCDESC: 405 if (sym)
394 » » » *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs 406 *reloc_addr = sym_val + addend;
395 » » » » + (def.sym - def.dso->syms)) : 0; 407 else
396 » » » break; 408 *reloc_addr = (size_t)base + addend;
397 » » case REL_FUNCDESC_VAL: 409 break;
398 » » » if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sy m_val; 410 case REL_COPY:
399 » » » else *reloc_addr = sym_val; 411 memcpy(reloc_addr, (void*)sym_val, sym->st_size);
400 » » » reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0; 412 break;
401 » » » break; 413 case REL_OFFSET32:
402 » » case REL_DTPMOD: 414 *(uint32_t*)reloc_addr = sym_val + addend - (size_t)reloc_addr;
403 » » » *reloc_addr = def.dso->tls_id; 415 break;
404 » » » break; 416 case REL_FUNCDESC:
405 » » case REL_DTPOFF: 417 *reloc_addr =
406 » » » *reloc_addr = tls_val + addend - DTP_OFFSET; 418 def.sym ? (size_t)(def.dso->funcdescs + (def.sym - def.dso->syms))
407 » » » break; 419 : 0;
420 break;
421 case REL_FUNCDESC_VAL:
422 if ((sym->st_info & 0xf) == STT_SECTION)
423 *reloc_addr += sym_val;
424 else
425 *reloc_addr = sym_val;
426 reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0;
427 break;
428 case REL_DTPMOD:
429 *reloc_addr = def.dso->tls_id;
430 break;
431 case REL_DTPOFF:
432 *reloc_addr = tls_val + addend - DTP_OFFSET;
433 break;
408 #ifdef TLS_ABOVE_TP 434 #ifdef TLS_ABOVE_TP
409 » » case REL_TPOFF: 435 case REL_TPOFF:
410 » » » *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend; 436 *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend;
411 » » » break; 437 break;
412 #else 438 #else
413 » » case REL_TPOFF: 439 case REL_TPOFF:
414 » » » *reloc_addr = tls_val - def.dso->tls.offset + addend; 440 *reloc_addr = tls_val - def.dso->tls.offset + addend;
415 » » » break; 441 break;
416 » » case REL_TPOFF_NEG: 442 case REL_TPOFF_NEG:
417 » » » *reloc_addr = def.dso->tls.offset - tls_val + addend; 443 *reloc_addr = def.dso->tls.offset - tls_val + addend;
418 » » » break; 444 break;
419 #endif 445 #endif
420 » » case REL_TLSDESC: 446 case REL_TLSDESC:
421 » » » if (stride<3) addend = reloc_addr[1]; 447 if (stride < 3)
422 » » » if (runtime && def.dso->tls_id >= static_tls_cnt) { 448 addend = reloc_addr[1];
423 » » » » struct td_index *new = malloc(sizeof *new); 449 if (runtime && def.dso->tls_id >= static_tls_cnt) {
424 » » » » if (!new) { 450 struct td_index* new = malloc(sizeof *new);
425 » » » » » error( 451 if (!new) {
426 » » » » » "Error relocating %s: cannot allocate TL SDESC for %s", 452 error("Error relocating %s: cannot allocate TLSDESC for %s",
427 » » » » » dso->name, sym ? name : "(local)" ); 453 dso->name, sym ? name : "(local)");
428 » » » » » longjmp(*rtld_fail, 1); 454 longjmp(*rtld_fail, 1);
429 » » » » } 455 }
430 » » » » new->next = dso->td_index; 456 new->next = dso->td_index;
431 » » » » dso->td_index = new; 457 dso->td_index = new;
432 » » » » new->args[0] = def.dso->tls_id; 458 new->args[0] = def.dso->tls_id;
433 » » » » new->args[1] = tls_val + addend; 459 new->args[1] = tls_val + addend;
434 » » » » reloc_addr[0] = (size_t)__tlsdesc_dynamic; 460 reloc_addr[0] = (size_t)__tlsdesc_dynamic;
435 » » » » reloc_addr[1] = (size_t)new; 461 reloc_addr[1] = (size_t) new;
436 » » » } else { 462 } else {
437 » » » » reloc_addr[0] = (size_t)__tlsdesc_static; 463 reloc_addr[0] = (size_t)__tlsdesc_static;
438 #ifdef TLS_ABOVE_TP 464 #ifdef TLS_ABOVE_TP
439 » » » » reloc_addr[1] = tls_val + def.dso->tls.offset 465 reloc_addr[1] = tls_val + def.dso->tls.offset + TPOFF_K + addend;
440 » » » » » + TPOFF_K + addend;
441 #else 466 #else
442 » » » » reloc_addr[1] = tls_val - def.dso->tls.offset 467 reloc_addr[1] = tls_val - def.dso->tls.offset + addend;
443 » » » » » + addend;
444 #endif 468 #endif
445 » » » } 469 }
446 » » » break; 470 break;
447 » » default: 471 default:
448 » » » error("Error relocating %s: unsupported relocation type %d", 472 error("Error relocating %s: unsupported relocation type %d", dso->name,
449 » » » » dso->name, type); 473 type);
450 » » » if (runtime) longjmp(*rtld_fail, 1); 474 if (runtime)
451 » » » continue; 475 longjmp(*rtld_fail, 1);
452 » » } 476 continue;
453 » } 477 }
478 }
454 } 479 }
455 480
456 /* A huge hack: to make up for the wastefulness of shared libraries 481 /* A huge hack: to make up for the wastefulness of shared libraries
457 * needing at least a page of dirty memory even if they have no global 482 * needing at least a page of dirty memory even if they have no global
458 * data, we reclaim the gaps at the beginning and end of writable maps 483 * data, we reclaim the gaps at the beginning and end of writable maps
459 * and "donate" them to the heap by setting up minimal malloc 484 * and "donate" them to the heap by setting up minimal malloc
460 * structures and then freeing them. */ 485 * structures and then freeing them. */
461 486
462 static void reclaim(struct dso *dso, size_t start, size_t end) 487 static void reclaim(struct dso* dso, size_t start, size_t end) {
463 { 488 size_t *a, *z;
464 size_t *a, *z; 489 if (start >= dso->relro_start && start < dso->relro_end)
465 if (start >= dso->relro_start && start < dso->relro_end) start = dso->re lro_end; 490 start = dso->relro_end;
466 if (end >= dso->relro_start && end < dso->relro_end) end = dso->relr o_start; 491 if (end >= dso->relro_start && end < dso->relro_end)
467 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t); 492 end = dso->relro_start;
468 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t); 493 start = start + 6 * sizeof(size_t) - 1 & -4 * sizeof(size_t);
469 if (start>end || end-start < 4*sizeof(size_t)) return; 494 end = (end & -4 * sizeof(size_t)) - 2 * sizeof(size_t);
470 a = laddr(dso, start); 495 if (start > end || end - start < 4 * sizeof(size_t))
471 z = laddr(dso, end); 496 return;
472 a[-2] = 1; 497 a = laddr(dso, start);
473 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1; 498 z = laddr(dso, end);
474 z[1] = 1; 499 a[-2] = 1;
475 free(a); 500 a[-1] = z[0] = end - start + 2 * sizeof(size_t) | 1;
476 } 501 z[1] = 1;
477 502 free(a);
478 static void reclaim_gaps(struct dso *dso) 503 }
479 { 504
480 Phdr *ph = dso->phdr; 505 static void reclaim_gaps(struct dso* dso) {
481 size_t phcnt = dso->phnum; 506 Phdr* ph = dso->phdr;
482 507 size_t phcnt = dso->phnum;
483 if (DL_FDPIC) return; // FIXME 508
484 for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) { 509 if (DL_FDPIC)
485 if (ph->p_type!=PT_LOAD) continue; 510 return; // FIXME
486 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue; 511 for (; phcnt--; ph = (void*)((char*)ph + dso->phentsize)) {
487 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr); 512 if (ph->p_type != PT_LOAD)
488 reclaim(dso, ph->p_vaddr+ph->p_memsz, 513 continue;
489 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE); 514 if ((ph->p_flags & (PF_R | PF_W)) != (PF_R | PF_W))
490 } 515 continue;
491 } 516 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
492 517 reclaim(dso, ph->p_vaddr + ph->p_memsz,
493 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t of f) 518 ph->p_vaddr + ph->p_memsz + PAGE_SIZE - 1 & -PAGE_SIZE);
494 { 519 }
495 static int no_map_fixed; 520 }
496 char *q; 521
497 if (!no_map_fixed) { 522 static void* mmap_fixed(void* p,
498 q = mmap(p, n, prot, flags|MAP_FIXED, fd, off); 523 size_t n,
499 if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL) 524 int prot,
500 return q; 525 int flags,
501 no_map_fixed = 1; 526 int fd,
502 } 527 off_t off) {
503 /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */ 528 static int no_map_fixed;
504 if (flags & MAP_ANONYMOUS) { 529 char* q;
505 memset(p, 0, n); 530 if (!no_map_fixed) {
506 return p; 531 q = mmap(p, n, prot, flags | MAP_FIXED, fd, off);
507 } 532 if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL)
508 ssize_t r; 533 return q;
509 if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED; 534 no_map_fixed = 1;
510 for (q=p; n; q+=r, off+=r, n-=r) { 535 }
511 r = read(fd, q, n); 536 /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
512 if (r < 0 && errno != EINTR) return MAP_FAILED; 537 if (flags & MAP_ANONYMOUS) {
513 if (!r) { 538 memset(p, 0, n);
514 memset(q, 0, n); 539 return p;
515 break; 540 }
516 } 541 ssize_t r;
517 } 542 if (lseek(fd, off, SEEK_SET) < 0)
518 return p; 543 return MAP_FAILED;
519 } 544 for (q = p; n; q += r, off += r, n -= r) {
520 545 r = read(fd, q, n);
521 static void unmap_library(struct dso *dso) 546 if (r < 0 && errno != EINTR)
522 { 547 return MAP_FAILED;
523 if (dso->loadmap) { 548 if (!r) {
524 size_t i; 549 memset(q, 0, n);
525 for (i=0; i<dso->loadmap->nsegs; i++) { 550 break;
526 if (!dso->loadmap->segs[i].p_memsz) 551 }
527 continue; 552 }
528 munmap((void *)dso->loadmap->segs[i].addr, 553 return p;
529 dso->loadmap->segs[i].p_memsz); 554 }
530 } 555
531 free(dso->loadmap); 556 static void unmap_library(struct dso* dso) {
532 } else if (dso->map && dso->map_len) { 557 if (dso->loadmap) {
533 munmap(dso->map, dso->map_len); 558 size_t i;
534 } 559 for (i = 0; i < dso->loadmap->nsegs; i++) {
535 } 560 if (!dso->loadmap->segs[i].p_memsz)
536 561 continue;
537 static void *map_library(int fd, struct dso *dso) 562 munmap((void*)dso->loadmap->segs[i].addr, dso->loadmap->segs[i].p_memsz);
538 { 563 }
539 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; 564 free(dso->loadmap);
540 void *allocated_buf=0; 565 } else if (dso->map && dso->map_len) {
541 size_t phsize; 566 munmap(dso->map, dso->map_len);
542 size_t addr_min=SIZE_MAX, addr_max=0, map_len; 567 }
543 size_t this_min, this_max; 568 }
544 size_t nsegs = 0; 569
545 off_t off_start; 570 static void* map_library(int fd, struct dso* dso) {
546 Ehdr *eh; 571 Ehdr buf[(896 + sizeof(Ehdr)) / sizeof(Ehdr)];
547 Phdr *ph, *ph0; 572 void* allocated_buf = 0;
548 unsigned prot; 573 size_t phsize;
549 unsigned char *map=MAP_FAILED, *base; 574 size_t addr_min = SIZE_MAX, addr_max = 0, map_len;
550 size_t dyn=0; 575 size_t this_min, this_max;
551 size_t tls_image=0; 576 size_t nsegs = 0;
552 size_t i; 577 off_t off_start;
553 578 Ehdr* eh;
554 ssize_t l = read(fd, buf, sizeof buf); 579 Phdr *ph, *ph0;
555 eh = buf; 580 unsigned prot;
556 if (l<0) return 0; 581 unsigned char *map = MAP_FAILED, *base;
557 if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC)) 582 size_t dyn = 0;
558 goto noexec; 583 size_t tls_image = 0;
559 phsize = eh->e_phentsize * eh->e_phnum; 584 size_t i;
560 if (phsize > sizeof buf - sizeof *eh) { 585
561 allocated_buf = malloc(phsize); 586 ssize_t l = read(fd, buf, sizeof buf);
562 if (!allocated_buf) return 0; 587 eh = buf;
563 l = pread(fd, allocated_buf, phsize, eh->e_phoff); 588 if (l < 0)
564 if (l < 0) goto error; 589 return 0;
565 if (l != phsize) goto noexec; 590 if (l < sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC))
566 ph = ph0 = allocated_buf; 591 goto noexec;
567 } else if (eh->e_phoff + phsize > l) { 592 phsize = eh->e_phentsize * eh->e_phnum;
568 l = pread(fd, buf+1, phsize, eh->e_phoff); 593 if (phsize > sizeof buf - sizeof *eh) {
569 if (l < 0) goto error; 594 allocated_buf = malloc(phsize);
570 if (l != phsize) goto noexec; 595 if (!allocated_buf)
571 ph = ph0 = (void *)(buf + 1); 596 return 0;
572 } else { 597 l = pread(fd, allocated_buf, phsize, eh->e_phoff);
573 ph = ph0 = (void *)((char *)buf + eh->e_phoff); 598 if (l < 0)
574 } 599 goto error;
575 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { 600 if (l != phsize)
576 if (ph->p_type == PT_DYNAMIC) { 601 goto noexec;
577 dyn = ph->p_vaddr; 602 ph = ph0 = allocated_buf;
578 } else if (ph->p_type == PT_TLS) { 603 } else if (eh->e_phoff + phsize > l) {
579 tls_image = ph->p_vaddr; 604 l = pread(fd, buf + 1, phsize, eh->e_phoff);
580 dso->tls.align = ph->p_align; 605 if (l < 0)
581 dso->tls.len = ph->p_filesz; 606 goto error;
582 dso->tls.size = ph->p_memsz; 607 if (l != phsize)
583 } else if (ph->p_type == PT_GNU_RELRO) { 608 goto noexec;
584 dso->relro_start = ph->p_vaddr & -PAGE_SIZE; 609 ph = ph0 = (void*)(buf + 1);
585 dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZ E; 610 } else {
586 } 611 ph = ph0 = (void*)((char*)buf + eh->e_phoff);
587 if (ph->p_type != PT_LOAD) continue; 612 }
588 nsegs++; 613 for (i = eh->e_phnum; i; i--, ph = (void*)((char*)ph + eh->e_phentsize)) {
589 if (ph->p_vaddr < addr_min) { 614 if (ph->p_type == PT_DYNAMIC) {
590 addr_min = ph->p_vaddr; 615 dyn = ph->p_vaddr;
591 off_start = ph->p_offset; 616 } else if (ph->p_type == PT_TLS) {
592 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | 617 tls_image = ph->p_vaddr;
593 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | 618 dso->tls.align = ph->p_align;
594 ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); 619 dso->tls.len = ph->p_filesz;
595 } 620 dso->tls.size = ph->p_memsz;
596 if (ph->p_vaddr+ph->p_memsz > addr_max) { 621 } else if (ph->p_type == PT_GNU_RELRO) {
597 addr_max = ph->p_vaddr+ph->p_memsz; 622 dso->relro_start = ph->p_vaddr & -PAGE_SIZE;
598 } 623 dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
599 } 624 }
600 if (!dyn) goto noexec; 625 if (ph->p_type != PT_LOAD)
601 if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) { 626 continue;
602 dso->loadmap = calloc(1, sizeof *dso->loadmap 627 nsegs++;
603 + nsegs * sizeof *dso->loadmap->segs); 628 if (ph->p_vaddr < addr_min) {
604 if (!dso->loadmap) goto error; 629 addr_min = ph->p_vaddr;
605 dso->loadmap->nsegs = nsegs; 630 off_start = ph->p_offset;
606 for (ph=ph0, i=0; i<nsegs; ph=(void *)((char *)ph+eh->e_phentsiz e)) { 631 prot = (((ph->p_flags & PF_R) ? PROT_READ : 0) |
607 if (ph->p_type != PT_LOAD) continue; 632 ((ph->p_flags & PF_W) ? PROT_WRITE : 0) |
608 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | 633 ((ph->p_flags & PF_X) ? PROT_EXEC : 0));
609 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | 634 }
610 ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); 635 if (ph->p_vaddr + ph->p_memsz > addr_max) {
611 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1), 636 addr_max = ph->p_vaddr + ph->p_memsz;
612 prot, MAP_PRIVATE, 637 }
613 fd, ph->p_offset & -PAGE_SIZE); 638 }
614 if (map == MAP_FAILED) { 639 if (!dyn)
615 unmap_library(dso); 640 goto noexec;
616 goto error; 641 if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
617 } 642 dso->loadmap =
618 dso->loadmap->segs[i].addr = (size_t)map + 643 calloc(1, sizeof *dso->loadmap + nsegs * sizeof *dso->loadmap->segs);
619 (ph->p_vaddr & PAGE_SIZE-1); 644 if (!dso->loadmap)
620 dso->loadmap->segs[i].p_vaddr = ph->p_vaddr; 645 goto error;
621 dso->loadmap->segs[i].p_memsz = ph->p_memsz; 646 dso->loadmap->nsegs = nsegs;
622 i++; 647 for (ph = ph0, i = 0; i < nsegs;
623 if (prot & PROT_WRITE) { 648 ph = (void*)((char*)ph + eh->e_phentsize)) {
624 size_t brk = (ph->p_vaddr & PAGE_SIZE-1) 649 if (ph->p_type != PT_LOAD)
625 + ph->p_filesz; 650 continue;
626 size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE; 651 prot = (((ph->p_flags & PF_R) ? PROT_READ : 0) |
627 size_t pgend = brk + ph->p_memsz - ph->p_filesz 652 ((ph->p_flags & PF_W) ? PROT_WRITE : 0) |
628 + PAGE_SIZE-1 & -PAGE_SIZE; 653 ((ph->p_flags & PF_X) ? PROT_EXEC : 0));
629 if (pgend > pgbrk && mmap_fixed(map+pgbrk, 654 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE - 1), prot,
630 pgend-pgbrk, prot, 655 MAP_PRIVATE, fd, ph->p_offset & -PAGE_SIZE);
631 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, 656 if (map == MAP_FAILED) {
632 -1, off_start) == MAP_FAILED) 657 unmap_library(dso);
633 goto error; 658 goto error;
634 memset(map + brk, 0, pgbrk-brk); 659 }
635 } 660 dso->loadmap->segs[i].addr = (size_t)map + (ph->p_vaddr & PAGE_SIZE - 1);
636 } 661 dso->loadmap->segs[i].p_vaddr = ph->p_vaddr;
637 map = (void *)dso->loadmap->segs[0].addr; 662 dso->loadmap->segs[i].p_memsz = ph->p_memsz;
638 map_len = 0; 663 i++;
639 goto done_mapping; 664 if (prot & PROT_WRITE) {
640 } 665 size_t brk = (ph->p_vaddr & PAGE_SIZE - 1) + ph->p_filesz;
641 addr_max += PAGE_SIZE-1; 666 size_t pgbrk = brk + PAGE_SIZE - 1 & -PAGE_SIZE;
642 addr_max &= -PAGE_SIZE; 667 size_t pgend =
643 addr_min &= -PAGE_SIZE; 668 brk + ph->p_memsz - ph->p_filesz + PAGE_SIZE - 1 & -PAGE_SIZE;
644 off_start &= -PAGE_SIZE; 669 if (pgend > pgbrk &&
645 map_len = addr_max - addr_min + off_start; 670 mmap_fixed(map + pgbrk, pgend - pgbrk, prot,
646 /* The first time, we map too much, possibly even more than 671 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1,
647 * the length of the file. This is okay because we will not 672 off_start) == MAP_FAILED)
648 * use the invalid part; we just need to reserve the right 673 goto error;
649 * amount of virtual address space to map over later. */ 674 memset(map + brk, 0, pgbrk - brk);
650 map = DL_NOMMU_SUPPORT 675 }
651 ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC , 676 }
652 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) 677 map = (void*)dso->loadmap->segs[0].addr;
653 : mmap((void *)addr_min, map_len, prot, 678 map_len = 0;
654 MAP_PRIVATE, fd, off_start); 679 goto done_mapping;
655 if (map==MAP_FAILED) goto error; 680 }
656 dso->map = map; 681 addr_max += PAGE_SIZE - 1;
657 dso->map_len = map_len; 682 addr_max &= -PAGE_SIZE;
658 /* If the loaded file is not relocatable and the requested address is 683 addr_min &= -PAGE_SIZE;
659 * not available, then the load operation must fail. */ 684 off_start &= -PAGE_SIZE;
660 if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) { 685 map_len = addr_max - addr_min + off_start;
661 errno = EBUSY; 686 /* The first time, we map too much, possibly even more than
662 goto error; 687 * the length of the file. This is okay because we will not
663 } 688 * use the invalid part; we just need to reserve the right
664 base = map - addr_min; 689 * amount of virtual address space to map over later. */
665 dso->phdr = 0; 690 map = DL_NOMMU_SUPPORT
666 dso->phnum = 0; 691 ? mmap((void*)addr_min, map_len, PROT_READ | PROT_WRITE | PROT_EXEC,
667 for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsi ze)) { 692 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
668 if (ph->p_type != PT_LOAD) continue; 693 : mmap((void*)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
669 /* Check if the programs headers are in this load segment, and 694 if (map == MAP_FAILED)
670 * if so, record the address for use by dl_iterate_phdr. */ 695 goto error;
671 if (!dso->phdr && eh->e_phoff >= ph->p_offset 696 dso->map = map;
672 && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) { 697 dso->map_len = map_len;
673 dso->phdr = (void *)(base + ph->p_vaddr 698 /* If the loaded file is not relocatable and the requested address is
674 + (eh->e_phoff-ph->p_offset)); 699 * not available, then the load operation must fail. */
675 dso->phnum = eh->e_phnum; 700 if (eh->e_type != ET_DYN && addr_min && map != (void*)addr_min) {
676 dso->phentsize = eh->e_phentsize; 701 errno = EBUSY;
677 } 702 goto error;
678 /* Reuse the existing mapping for the lowest-address LOAD */ 703 }
679 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min && !DL_NOMMU_SUPPORT) 704 base = map - addr_min;
680 continue; 705 dso->phdr = 0;
681 this_min = ph->p_vaddr & -PAGE_SIZE; 706 dso->phnum = 0;
682 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; 707 for (ph = ph0, i = eh->e_phnum; i;
683 off_start = ph->p_offset & -PAGE_SIZE; 708 i--, ph = (void*)((char*)ph + eh->e_phentsize)) {
684 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | 709 if (ph->p_type != PT_LOAD)
685 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | 710 continue;
686 ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); 711 /* Check if the programs headers are in this load segment, and
687 if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVA TE|MAP_FIXED, fd, off_start) == MAP_FAILED) 712 * if so, record the address for use by dl_iterate_phdr. */
688 goto error; 713 if (!dso->phdr && eh->e_phoff >= ph->p_offset &&
689 if (ph->p_memsz > ph->p_filesz) { 714 eh->e_phoff + phsize <= ph->p_offset + ph->p_filesz) {
690 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; 715 dso->phdr = (void*)(base + ph->p_vaddr + (eh->e_phoff - ph->p_offset));
691 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; 716 dso->phnum = eh->e_phnum;
692 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); 717 dso->phentsize = eh->e_phentsize;
693 if (pgbrk-(size_t)base < this_max && mmap_fixed((void *) pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, - 1, 0) == MAP_FAILED) 718 }
694 goto error; 719 /* Reuse the existing mapping for the lowest-address LOAD */
695 } 720 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min && !DL_NOMMU_SUPPORT)
696 } 721 continue;
697 for (i=0; ((size_t *)(base+dyn))[i]; i+=2) 722 this_min = ph->p_vaddr & -PAGE_SIZE;
698 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) { 723 this_max = ph->p_vaddr + ph->p_memsz + PAGE_SIZE - 1 & -PAGE_SIZE;
699 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXE C) 724 off_start = ph->p_offset & -PAGE_SIZE;
700 && errno != ENOSYS) 725 prot = (((ph->p_flags & PF_R) ? PROT_READ : 0) |
701 goto error; 726 ((ph->p_flags & PF_W) ? PROT_WRITE : 0) |
702 break; 727 ((ph->p_flags & PF_X) ? PROT_EXEC : 0));
703 } 728 if (mmap_fixed(base + this_min, this_max - this_min, prot,
729 MAP_PRIVATE | MAP_FIXED, fd, off_start) == MAP_FAILED)
730 goto error;
731 if (ph->p_memsz > ph->p_filesz) {
732 size_t brk = (size_t)base + ph->p_vaddr + ph->p_filesz;
733 size_t pgbrk = brk + PAGE_SIZE - 1 & -PAGE_SIZE;
734 memset((void*)brk, 0, pgbrk - brk & PAGE_SIZE - 1);
735 if (pgbrk - (size_t)base < this_max &&
736 mmap_fixed((void*)pgbrk, (size_t)base + this_max - pgbrk, prot,
737 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1,
738 0) == MAP_FAILED)
739 goto error;
740 }
741 }
742 for (i = 0; ((size_t*)(base + dyn))[i]; i += 2)
743 if (((size_t*)(base + dyn))[i] == DT_TEXTREL) {
744 if (mprotect(map, map_len, PROT_READ | PROT_WRITE | PROT_EXEC) &&
745 errno != ENOSYS)
746 goto error;
747 break;
748 }
704 done_mapping: 749 done_mapping:
705 » dso->base = base; 750 dso->base = base;
706 » dso->dynv = laddr(dso, dyn); 751 dso->dynv = laddr(dso, dyn);
707 » if (dso->tls.size) dso->tls.image = laddr(dso, tls_image); 752 if (dso->tls.size)
708 » if (!runtime) reclaim_gaps(dso); 753 dso->tls.image = laddr(dso, tls_image);
709 » free(allocated_buf); 754 if (!runtime)
710 » return map; 755 reclaim_gaps(dso);
756 free(allocated_buf);
757 return map;
711 noexec: 758 noexec:
712 » errno = ENOEXEC; 759 errno = ENOEXEC;
713 error: 760 error:
714 » if (map!=MAP_FAILED) unmap_library(dso); 761 if (map != MAP_FAILED)
715 » free(allocated_buf); 762 unmap_library(dso);
716 » return 0; 763 free(allocated_buf);
717 } 764 return 0;
718 765 }
719 static int path_open(const char *name, const char *s, char *buf, size_t buf_size ) 766
720 { 767 static int path_open(const char* name,
721 » size_t l; 768 const char* s,
722 » int fd; 769 char* buf,
723 » for (;;) { 770 size_t buf_size) {
724 » » s += strspn(s, ":\n"); 771 size_t l;
725 » » l = strcspn(s, ":\n"); 772 int fd;
726 » » if (l-1 >= INT_MAX) return -1; 773 for (;;) {
727 » » if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_si ze) { 774 s += strspn(s, ":\n");
728 » » » if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd; 775 l = strcspn(s, ":\n");
729 » » » switch (errno) { 776 if (l - 1 >= INT_MAX)
730 » » » case ENOENT: 777 return -1;
731 » » » case ENOTDIR: 778 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) {
732 » » » case EACCES: 779 if ((fd = open(buf, O_RDONLY | O_CLOEXEC)) >= 0)
733 » » » case ENAMETOOLONG: 780 return fd;
734 » » » » break; 781 switch (errno) {
735 » » » default: 782 case ENOENT:
736 » » » » /* Any negative value but -1 will inhibit 783 case ENOTDIR:
737 » » » » * futher path search. */ 784 case EACCES:
738 » » » » return -2; 785 case ENAMETOOLONG:
739 » » » } 786 break;
740 » » } 787 default:
741 » » s += l; 788 /* Any negative value but -1 will inhibit
742 » } 789 * futher path search. */
743 } 790 return -2;
744 791 }
745 static int fixup_rpath(struct dso *p, char *buf, size_t buf_size) 792 }
746 { 793 s += l;
747 » size_t n, l; 794 }
748 » const char *s, *t, *origin; 795 }
749 » char *d; 796
750 » if (p->rpath || !p->rpath_orig) return 0; 797 static int fixup_rpath(struct dso* p, char* buf, size_t buf_size) {
751 » if (!strchr(p->rpath_orig, '$')) { 798 size_t n, l;
752 » » p->rpath = p->rpath_orig; 799 const char *s, *t, *origin;
753 » » return 0; 800 char* d;
754 » } 801 if (p->rpath || !p->rpath_orig)
755 » n = 0; 802 return 0;
756 » s = p->rpath_orig; 803 if (!strchr(p->rpath_orig, '$')) {
757 » while ((t=strchr(s, '$'))) { 804 p->rpath = p->rpath_orig;
758 » » if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9)) 805 return 0;
759 » » » return 0; 806 }
760 » » s = t+1; 807 n = 0;
761 » » n++; 808 s = p->rpath_orig;
762 » } 809 while ((t = strchr(s, '$'))) {
763 » if (n > SSIZE_MAX/PATH_MAX) return 0; 810 if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9))
764 811 return 0;
765 » if (p->kernel_mapped) { 812 s = t + 1;
766 » » /* $ORIGIN searches cannot be performed for the main program 813 n++;
767 » » * when it is suid/sgid/AT_SECURE. This is because the 814 }
768 » » * pathname is under the control of the caller of execve. 815 if (n > SSIZE_MAX / PATH_MAX)
769 » » * For libraries, however, $ORIGIN can be processed safely 816 return 0;
770 » » * since the library's pathname came from a trusted source 817
771 » » * (either system paths or a call to dlopen). */ 818 if (p->kernel_mapped) {
772 » » if (libc.secure) 819 /* $ORIGIN searches cannot be performed for the main program
773 » » » return 0; 820 * when it is suid/sgid/AT_SECURE. This is because the
774 » » l = readlink("/proc/self/exe", buf, buf_size); 821 * pathname is under the control of the caller of execve.
775 » » if (l == -1) switch (errno) { 822 * For libraries, however, $ORIGIN can be processed safely
776 » » case ENOENT: 823 * since the library's pathname came from a trusted source
777 » » case ENOTDIR: 824 * (either system paths or a call to dlopen). */
778 » » case EACCES: 825 if (libc.secure)
779 » » » break; 826 return 0;
780 » » default: 827 l = readlink("/proc/self/exe", buf, buf_size);
781 » » » return -1; 828 if (l == -1)
782 » » } 829 switch (errno) {
783 » » if (l >= buf_size) 830 case ENOENT:
784 » » » return 0; 831 case ENOTDIR:
785 » » buf[l] = 0; 832 case EACCES:
786 » » origin = buf; 833 break;
787 » } else { 834 default:
788 » » origin = p->name; 835 return -1;
789 » } 836 }
790 » t = strrchr(origin, '/'); 837 if (l >= buf_size)
791 » l = t ? t-origin : 0; 838 return 0;
792 » p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1); 839 buf[l] = 0;
793 » if (!p->rpath) return -1; 840 origin = buf;
794 841 } else {
795 » d = p->rpath; 842 origin = p->name;
796 » s = p->rpath_orig; 843 }
797 » while ((t=strchr(s, '$'))) { 844 t = strrchr(origin, '/');
798 » » memcpy(d, s, t-s); 845 l = t ? t - origin : 0;
799 » » d += t-s; 846 p->rpath = malloc(strlen(p->rpath_orig) + n * l + 1);
800 » » memcpy(d, origin, l); 847 if (!p->rpath)
801 » » d += l; 848 return -1;
802 » » /* It was determined previously that the '$' is followed 849
803 » » * either by "ORIGIN" or "{ORIGIN}". */ 850 d = p->rpath;
804 » » s = t + 7 + 2*(t[1]=='{'); 851 s = p->rpath_orig;
805 » } 852 while ((t = strchr(s, '$'))) {
806 » strcpy(d, s); 853 memcpy(d, s, t - s);
807 » return 0; 854 d += t - s;
808 } 855 memcpy(d, origin, l);
809 856 d += l;
810 static void decode_dyn(struct dso *p) 857 /* It was determined previously that the '$' is followed
811 { 858 * either by "ORIGIN" or "{ORIGIN}". */
812 » size_t dyn[DYN_CNT]; 859 s = t + 7 + 2 * (t[1] == '{');
813 » decode_vec(p->dynv, dyn, DYN_CNT); 860 }
814 » p->syms = laddr(p, dyn[DT_SYMTAB]); 861 strcpy(d, s);
815 » p->strings = laddr(p, dyn[DT_STRTAB]); 862 return 0;
816 » if (dyn[0]&(1<<DT_HASH)) 863 }
817 » » p->hashtab = laddr(p, dyn[DT_HASH]); 864
818 » if (dyn[0]&(1<<DT_RPATH)) 865 static void decode_dyn(struct dso* p) {
819 » » p->rpath_orig = p->strings + dyn[DT_RPATH]; 866 size_t dyn[DYN_CNT];
820 » if (dyn[0]&(1<<DT_RUNPATH)) 867 decode_vec(p->dynv, dyn, DYN_CNT);
821 » » p->rpath_orig = p->strings + dyn[DT_RUNPATH]; 868 p->syms = laddr(p, dyn[DT_SYMTAB]);
822 » if (dyn[0]&(1<<DT_PLTGOT)) 869 p->strings = laddr(p, dyn[DT_STRTAB]);
823 » » p->got = laddr(p, dyn[DT_PLTGOT]); 870 if (dyn[0] & (1 << DT_HASH))
824 » if (search_vec(p->dynv, dyn, DT_GNU_HASH)) 871 p->hashtab = laddr(p, dyn[DT_HASH]);
825 » » p->ghashtab = laddr(p, *dyn); 872 if (dyn[0] & (1 << DT_RPATH))
826 » if (search_vec(p->dynv, dyn, DT_VERSYM)) 873 p->rpath_orig = p->strings + dyn[DT_RPATH];
827 » » p->versym = laddr(p, *dyn); 874 if (dyn[0] & (1 << DT_RUNPATH))
828 } 875 p->rpath_orig = p->strings + dyn[DT_RUNPATH];
829 876 if (dyn[0] & (1 << DT_PLTGOT))
830 static size_t count_syms(struct dso *p) 877 p->got = laddr(p, dyn[DT_PLTGOT]);
831 { 878 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
832 » if (p->hashtab) return p->hashtab[1]; 879 p->ghashtab = laddr(p, *dyn);
833 880 if (search_vec(p->dynv, dyn, DT_VERSYM))
834 » size_t nsym, i; 881 p->versym = laddr(p, *dyn);
835 » uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4); 882 }
836 » uint32_t *hashval; 883
837 » for (i = nsym = 0; i < p->ghashtab[0]; i++) { 884 static size_t count_syms(struct dso* p) {
838 » » if (buckets[i] > nsym) 885 if (p->hashtab)
839 » » » nsym = buckets[i]; 886 return p->hashtab[1];
840 » } 887
841 » if (nsym) { 888 size_t nsym, i;
842 » » hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]); 889 uint32_t* buckets = p->ghashtab + 4 + (p->ghashtab[2] * sizeof(size_t) / 4);
843 » » do nsym++; 890 uint32_t* hashval;
844 » » while (!(*hashval++ & 1)); 891 for (i = nsym = 0; i < p->ghashtab[0]; i++) {
845 » } 892 if (buckets[i] > nsym)
846 » return nsym; 893 nsym = buckets[i];
847 } 894 }
848 895 if (nsym) {
849 static void *dl_mmap(size_t n) 896 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]);
850 { 897 do
851 » void *p; 898 nsym++;
852 » int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE; 899 while (!(*hashval++ & 1));
900 }
901 return nsym;
902 }
903
904 static void* dl_mmap(size_t n) {
905 void* p;
906 int prot = PROT_READ | PROT_WRITE, flags = MAP_ANONYMOUS | MAP_PRIVATE;
853 #ifdef SYS_mmap2 907 #ifdef SYS_mmap2
854 » p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0); 908 p = (void*)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0);
855 #else 909 #else
856 » p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0); 910 p = (void*)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0);
857 #endif 911 #endif
858 return p == MAP_FAILED ? 0 : p; 912 return p == MAP_FAILED ? 0 : p;
859 } 913 }
860 914
861 static void makefuncdescs(struct dso *p) 915 static void makefuncdescs(struct dso* p) {
862 { 916 static int self_done;
863 static int self_done; 917 size_t nsym = count_syms(p);
864 size_t nsym = count_syms(p); 918 size_t i, size = nsym * sizeof(*p->funcdescs);
865 size_t i, size = nsym * sizeof(*p->funcdescs); 919
866 920 if (!self_done) {
867 if (!self_done) { 921 p->funcdescs = dl_mmap(size);
868 p->funcdescs = dl_mmap(size); 922 self_done = 1;
869 self_done = 1; 923 } else {
870 } else { 924 p->funcdescs = malloc(size);
871 p->funcdescs = malloc(size); 925 }
872 } 926 if (!p->funcdescs) {
873 if (!p->funcdescs) { 927 if (!runtime)
874 if (!runtime) a_crash(); 928 a_crash();
875 error("Error allocating function descriptors for %s", p->name); 929 error("Error allocating function descriptors for %s", p->name);
876 longjmp(*rtld_fail, 1); 930 longjmp(*rtld_fail, 1);
877 } 931 }
878 for (i=0; i<nsym; i++) { 932 for (i = 0; i < nsym; i++) {
879 if ((p->syms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) { 933 if ((p->syms[i].st_info & 0xf) == STT_FUNC && p->syms[i].st_shndx) {
880 p->funcdescs[i].addr = laddr(p, p->syms[i].st_value); 934 p->funcdescs[i].addr = laddr(p, p->syms[i].st_value);
881 p->funcdescs[i].got = p->got; 935 p->funcdescs[i].got = p->got;
882 } else { 936 } else {
883 p->funcdescs[i].addr = 0; 937 p->funcdescs[i].addr = 0;
884 p->funcdescs[i].got = 0; 938 p->funcdescs[i].got = 0;
885 } 939 }
886 } 940 }
887 } 941 }
888 942
889 static struct dso *load_library(const char *name, struct dso *needed_by) 943 static struct dso* load_library(const char* name, struct dso* needed_by) {
890 { 944 char buf[2 * NAME_MAX + 2];
891 char buf[2*NAME_MAX+2]; 945 const char* pathname;
892 const char *pathname; 946 unsigned char* map;
893 unsigned char *map; 947 struct dso *p, temp_dso = {0};
894 struct dso *p, temp_dso = {0}; 948 int fd;
895 int fd; 949 struct stat st;
896 struct stat st; 950 size_t alloc_size;
897 size_t alloc_size; 951 int n_th = 0;
898 int n_th = 0; 952 int is_self = 0;
899 int is_self = 0; 953
900 954 if (!*name) {
901 if (!*name) { 955 errno = EINVAL;
902 errno = EINVAL; 956 return 0;
903 return 0; 957 }
904 } 958
905 959 /* Catch and block attempts to reload the implementation itself */
906 /* Catch and block attempts to reload the implementation itself */ 960 if (name[0] == 'l' && name[1] == 'i' && name[2] == 'b') {
907 if (name[0]=='l' && name[1]=='i' && name[2]=='b') { 961 static const char *rp, reserved[] = "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
908 static const char *rp, reserved[] = 962 char* z = strchr(name, '.');
909 "c\0pthread\0rt\0m\0dl\0util\0xnet\0"; 963 if (z) {
910 char *z = strchr(name, '.'); 964 size_t l = z - name;
911 if (z) { 965 for (rp = reserved; *rp && strncmp(name + 3, rp, l - 3);
912 size_t l = z-name; 966 rp += strlen(rp) + 1)
913 for (rp=reserved; *rp && strncmp(name+3, rp, l-3); rp+=s trlen(rp)+1); 967 ;
914 if (*rp) { 968 if (*rp) {
915 if (ldd_mode) { 969 if (ldd_mode) {
916 /* Track which names have been resolved 970 /* Track which names have been resolved
917 * and only report each one once. */ 971 * and only report each one once. */
918 static unsigned reported; 972 static unsigned reported;
919 unsigned mask = 1U<<(rp-reserved); 973 unsigned mask = 1U << (rp - reserved);
920 if (!(reported & mask)) { 974 if (!(reported & mask)) {
921 reported |= mask; 975 reported |= mask;
922 dprintf(1, "\t%s => %s (%p)\n", 976 dprintf(1, "\t%s => %s (%p)\n", name, ldso.name, ldso.base);
923 name, ldso.name, 977 }
924 ldso.base); 978 }
925 } 979 is_self = 1;
926 } 980 }
927 is_self = 1; 981 }
928 } 982 }
929 } 983 if (!strcmp(name, ldso.name))
930 } 984 is_self = 1;
931 if (!strcmp(name, ldso.name)) is_self = 1; 985 if (is_self) {
932 if (is_self) { 986 if (!ldso.prev) {
933 if (!ldso.prev) { 987 tail->next = &ldso;
934 tail->next = &ldso; 988 ldso.prev = tail;
935 ldso.prev = tail; 989 tail = ldso.next ? ldso.next : &ldso;
936 tail = ldso.next ? ldso.next : &ldso; 990 }
937 } 991 return &ldso;
938 return &ldso; 992 }
939 } 993 if (strchr(name, '/')) {
940 if (strchr(name, '/')) { 994 pathname = name;
941 pathname = name; 995 fd = open(name, O_RDONLY | O_CLOEXEC);
942 fd = open(name, O_RDONLY|O_CLOEXEC); 996 } else {
943 } else { 997 /* Search for the name to see if it's already loaded */
944 /* Search for the name to see if it's already loaded */ 998 for (p = head->next; p; p = p->next) {
945 for (p=head->next; p; p=p->next) { 999 if (p->shortname && !strcmp(p->shortname, name)) {
946 if (p->shortname && !strcmp(p->shortname, name)) { 1000 p->refcnt++;
947 p->refcnt++; 1001 return p;
948 return p; 1002 }
949 } 1003 }
950 } 1004 if (strlen(name) > NAME_MAX)
951 if (strlen(name) > NAME_MAX) return 0; 1005 return 0;
952 fd = -1; 1006 fd = -1;
953 if (env_path) fd = path_open(name, env_path, buf, sizeof buf); 1007 if (env_path)
954 for (p=needed_by; fd == -1 && p; p=p->needed_by) { 1008 fd = path_open(name, env_path, buf, sizeof buf);
955 if (fixup_rpath(p, buf, sizeof buf) < 0) 1009 for (p = needed_by; fd == -1 && p; p = p->needed_by) {
956 fd = -2; /* Inhibit further search. */ 1010 if (fixup_rpath(p, buf, sizeof buf) < 0)
957 if (p->rpath) 1011 fd = -2; /* Inhibit further search. */
958 fd = path_open(name, p->rpath, buf, sizeof buf); 1012 if (p->rpath)
959 } 1013 fd = path_open(name, p->rpath, buf, sizeof buf);
960 if (fd == -1) { 1014 }
961 if (!sys_path) { 1015 if (fd == -1) {
962 char *prefix = 0; 1016 if (!sys_path) {
963 size_t prefix_len; 1017 char* prefix = 0;
964 if (ldso.name[0]=='/') { 1018 size_t prefix_len;
965 char *s, *t, *z; 1019 if (ldso.name[0] == '/') {
966 for (s=t=z=ldso.name; *s; s++) 1020 char *s, *t, *z;
967 if (*s=='/') z=t, t=s; 1021 for (s = t = z = ldso.name; *s; s++)
968 prefix_len = z-ldso.name; 1022 if (*s == '/')
969 if (prefix_len < PATH_MAX) 1023 z = t, t = s;
970 prefix = ldso.name; 1024 prefix_len = z - ldso.name;
971 } 1025 if (prefix_len < PATH_MAX)
972 if (!prefix) { 1026 prefix = ldso.name;
973 prefix = ""; 1027 }
974 prefix_len = 0; 1028 if (!prefix) {
975 } 1029 prefix = "";
976 char etc_ldso_path[prefix_len + 1 1030 prefix_len = 0;
977 + sizeof "/etc/ld-musl-" LDSO_ARCH ".pat h"]; 1031 }
978 snprintf(etc_ldso_path, sizeof etc_ldso_path, 1032 char etc_ldso_path[prefix_len + 1 + sizeof "/etc/ld-musl-" LDSO_ARCH
979 "%.*s/etc/ld-musl-" LDSO_ARCH ".path", 1033 ".path"];
980 (int)prefix_len, prefix); 1034 snprintf(etc_ldso_path, sizeof etc_ldso_path,
981 FILE *f = fopen(etc_ldso_path, "rbe"); 1035 "%.*s/etc/ld-musl-" LDSO_ARCH ".path", (int)prefix_len,
982 if (f) { 1036 prefix);
983 if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) { 1037 FILE* f = fopen(etc_ldso_path, "rbe");
984 free(sys_path); 1038 if (f) {
985 sys_path = ""; 1039 if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
986 } 1040 free(sys_path);
987 fclose(f); 1041 sys_path = "";
988 } else if (errno != ENOENT) { 1042 }
989 sys_path = ""; 1043 fclose(f);
990 } 1044 } else if (errno != ENOENT) {
991 } 1045 sys_path = "";
992 if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib" ; 1046 }
993 fd = path_open(name, sys_path, buf, sizeof buf); 1047 }
994 } 1048 if (!sys_path)
995 pathname = buf; 1049 sys_path = "/lib:/usr/local/lib:/usr/lib";
996 } 1050 fd = path_open(name, sys_path, buf, sizeof buf);
997 if (fd < 0) return 0; 1051 }
998 if (fstat(fd, &st) < 0) { 1052 pathname = buf;
999 close(fd); 1053 }
1000 return 0; 1054 if (fd < 0)
1001 } 1055 return 0;
1002 for (p=head->next; p; p=p->next) { 1056 if (fstat(fd, &st) < 0) {
1003 if (p->dev == st.st_dev && p->ino == st.st_ino) { 1057 close(fd);
1004 /* If this library was previously loaded with a 1058 return 0;
1005 * pathname but a search found the same inode, 1059 }
1006 * setup its shortname so it can be found by name. */ 1060 for (p = head->next; p; p = p->next) {
1007 if (!p->shortname && pathname != name) 1061 if (p->dev == st.st_dev && p->ino == st.st_ino) {
1008 p->shortname = strrchr(p->name, '/')+1; 1062 /* If this library was previously loaded with a
1009 close(fd); 1063 * pathname but a search found the same inode,
1010 p->refcnt++; 1064 * setup its shortname so it can be found by name. */
1011 return p; 1065 if (!p->shortname && pathname != name)
1012 } 1066 p->shortname = strrchr(p->name, '/') + 1;
1013 } 1067 close(fd);
1014 map = noload ? 0 : map_library(fd, &temp_dso); 1068 p->refcnt++;
1015 close(fd); 1069 return p;
1016 if (!map) return 0; 1070 }
1017 1071 }
1018 /* Allocate storage for the new DSO. When there is TLS, this 1072 map = noload ? 0 : map_library(fd, &temp_dso);
1019 * storage must include a reservation for all pre-existing 1073 close(fd);
1020 * threads to obtain copies of both the new TLS, and an 1074 if (!map)
1021 * extended DTV capable of storing an additional slot for 1075 return 0;
1022 * the newly-loaded DSO. */ 1076
1023 alloc_size = sizeof *p + strlen(pathname) + 1; 1077 /* Allocate storage for the new DSO. When there is TLS, this
1024 if (runtime && temp_dso.tls.image) { 1078 * storage must include a reservation for all pre-existing
1025 size_t per_th = temp_dso.tls.size + temp_dso.tls.align 1079 * threads to obtain copies of both the new TLS, and an
1026 + sizeof(void *) * (tls_cnt+3); 1080 * extended DTV capable of storing an additional slot for
1027 n_th = libc.threads_minus_1 + 1; 1081 * the newly-loaded DSO. */
1028 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX; 1082 alloc_size = sizeof *p + strlen(pathname) + 1;
1029 else alloc_size += n_th * per_th; 1083 if (runtime && temp_dso.tls.image) {
1030 } 1084 size_t per_th =
1031 p = calloc(1, alloc_size); 1085 temp_dso.tls.size + temp_dso.tls.align + sizeof(void*) * (tls_cnt + 3);
1032 if (!p) { 1086 n_th = libc.threads_minus_1 + 1;
1033 unmap_library(&temp_dso); 1087 if (n_th > SSIZE_MAX / per_th)
1034 return 0; 1088 alloc_size = SIZE_MAX;
1035 } 1089 else
1036 memcpy(p, &temp_dso, sizeof temp_dso); 1090 alloc_size += n_th * per_th;
1037 decode_dyn(p); 1091 }
1038 p->dev = st.st_dev; 1092 p = calloc(1, alloc_size);
1039 p->ino = st.st_ino; 1093 if (!p) {
1040 p->refcnt = 1; 1094 unmap_library(&temp_dso);
1041 p->needed_by = needed_by; 1095 return 0;
1042 p->name = p->buf; 1096 }
1043 strcpy(p->name, pathname); 1097 memcpy(p, &temp_dso, sizeof temp_dso);
1044 /* Add a shortname only if name arg was not an explicit pathname. */ 1098 decode_dyn(p);
1045 if (pathname != name) p->shortname = strrchr(p->name, '/')+1; 1099 p->dev = st.st_dev;
1046 if (p->tls.image) { 1100 p->ino = st.st_ino;
1047 p->tls_id = ++tls_cnt; 1101 p->refcnt = 1;
1048 tls_align = MAXP2(tls_align, p->tls.align); 1102 p->needed_by = needed_by;
1103 p->name = p->buf;
1104 strcpy(p->name, pathname);
1105 /* Add a shortname only if name arg was not an explicit pathname. */
1106 if (pathname != name)
1107 p->shortname = strrchr(p->name, '/') + 1;
1108 if (p->tls.image) {
1109 p->tls_id = ++tls_cnt;
1110 tls_align = MAXP2(tls_align, p->tls.align);
1049 #ifdef TLS_ABOVE_TP 1111 #ifdef TLS_ABOVE_TP
1050 » » p->tls.offset = tls_offset + ( (tls_align-1) & 1112 p->tls.offset = tls_offset +
1051 » » » -(tls_offset + (uintptr_t)p->tls.image) ); 1113 ((tls_align - 1) & -(tls_offset + (uintptr_t)p->tls.image));
1052 » » tls_offset += p->tls.size; 1114 tls_offset += p->tls.size;
1053 #else 1115 #else
1054 » » tls_offset += p->tls.size + p->tls.align - 1; 1116 tls_offset += p->tls.size + p->tls.align - 1;
1055 » » tls_offset -= (tls_offset + (uintptr_t)p->tls.image) 1117 tls_offset -= (tls_offset + (uintptr_t)p->tls.image) & (p->tls.align - 1);
1056 » » » & (p->tls.align-1); 1118 p->tls.offset = tls_offset;
1057 » » p->tls.offset = tls_offset;
1058 #endif 1119 #endif
1059 » » p->new_dtv = (void *)(-sizeof(size_t) & 1120 p->new_dtv =
1060 » » » (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t))); 1121 (void*)(-sizeof(size_t) &
1061 » » p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1)); 1122 (uintptr_t)(p->name + strlen(p->name) + sizeof(size_t)));
1062 » » if (tls_tail) tls_tail->next = &p->tls; 1123 p->new_tls = (void*)(p->new_dtv + n_th * (tls_cnt + 1));
1063 » » else libc.tls_head = &p->tls; 1124 if (tls_tail)
1064 » » tls_tail = &p->tls; 1125 tls_tail->next = &p->tls;
1065 » } 1126 else
1066 1127 libc.tls_head = &p->tls;
1067 » tail->next = p; 1128 tls_tail = &p->tls;
1068 » p->prev = tail; 1129 }
1069 » tail = p; 1130
1070 1131 tail->next = p;
1071 » if (DL_FDPIC) makefuncdescs(p); 1132 p->prev = tail;
1072 1133 tail = p;
1073 » if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); 1134
1074 1135 if (DL_FDPIC)
1075 » return p; 1136 makefuncdescs(p);
1076 } 1137
1077 1138 if (ldd_mode)
1078 static void load_deps(struct dso *p) 1139 dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
1079 { 1140
1080 » size_t i, ndeps=0; 1141 return p;
1081 » struct dso ***deps = &p->deps, **tmp, *dep; 1142 }
1082 » for (; p; p=p->next) { 1143
1083 » » for (i=0; p->dynv[i]; i+=2) { 1144 static void load_deps(struct dso* p) {
1084 » » » if (p->dynv[i] != DT_NEEDED) continue; 1145 size_t i, ndeps = 0;
1085 » » » dep = load_library(p->strings + p->dynv[i+1], p); 1146 struct dso ***deps = &p->deps, **tmp, *dep;
1086 » » » if (!dep) { 1147 for (; p; p = p->next) {
1087 » » » » error("Error loading shared library %s: %m (need ed by %s)", 1148 for (i = 0; p->dynv[i]; i += 2) {
1088 » » » » » p->strings + p->dynv[i+1], p->name); 1149 if (p->dynv[i] != DT_NEEDED)
1089 » » » » if (runtime) longjmp(*rtld_fail, 1); 1150 continue;
1090 » » » » continue; 1151 dep = load_library(p->strings + p->dynv[i + 1], p);
1091 » » » } 1152 if (!dep) {
1092 » » » if (runtime) { 1153 error("Error loading shared library %s: %m (needed by %s)",
1093 » » » » tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2)); 1154 p->strings + p->dynv[i + 1], p->name);
1094 » » » » if (!tmp) longjmp(*rtld_fail, 1); 1155 if (runtime)
1095 » » » » tmp[ndeps++] = dep; 1156 longjmp(*rtld_fail, 1);
1096 » » » » tmp[ndeps] = 0; 1157 continue;
1097 » » » » *deps = tmp; 1158 }
1098 » » » } 1159 if (runtime) {
1099 » » } 1160 tmp = realloc(*deps, sizeof(*tmp) * (ndeps + 2));
1100 » } 1161 if (!tmp)
1101 } 1162 longjmp(*rtld_fail, 1);
1102 1163 tmp[ndeps++] = dep;
1103 static void load_preload(char *s) 1164 tmp[ndeps] = 0;
1104 { 1165 *deps = tmp;
1105 » int tmp; 1166 }
1106 » char *z; 1167 }
1107 » for (z=s; *z; s=z) { 1168 }
1108 » » for ( ; *s && (isspace(*s) || *s==':'); s++); 1169 }
1109 » » for (z=s; *z && !isspace(*z) && *z!=':'; z++); 1170
1110 » » tmp = *z; 1171 static void load_preload(char* s) {
1111 » » *z = 0; 1172 int tmp;
1112 » » load_library(s, 0); 1173 char* z;
1113 » » *z = tmp; 1174 for (z = s; *z; s = z) {
1114 » } 1175 for (; *s && (isspace(*s) || *s == ':'); s++)
1115 } 1176 ;
1116 1177 for (z = s; *z && !isspace(*z) && *z != ':'; z++)
1117 static void make_global(struct dso *p) 1178 ;
1118 { 1179 tmp = *z;
1119 » for (; p; p=p->next) p->global = 1; 1180 *z = 0;
1120 } 1181 load_library(s, 0);
1121 1182 *z = tmp;
1122 static void do_mips_relocs(struct dso *p, size_t *got) 1183 }
1123 { 1184 }
1124 » size_t i, j, rel[2]; 1185
1125 » unsigned char *base = p->base; 1186 static void make_global(struct dso* p) {
1126 » i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO); 1187 for (; p; p = p->next)
1127 » if (p==&ldso) { 1188 p->global = 1;
1128 » » got += i; 1189 }
1129 » } else { 1190
1130 » » while (i--) *got++ += (size_t)base; 1191 static void do_mips_relocs(struct dso* p, size_t* got) {
1131 » } 1192 size_t i, j, rel[2];
1132 » j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM); 1193 unsigned char* base = p->base;
1133 » i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); 1194 i = 0;
1134 » Sym *sym = p->syms + j; 1195 search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO);
1135 » rel[0] = (unsigned char *)got - base; 1196 if (p == &ldso) {
1136 » for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) { 1197 got += i;
1137 » » rel[1] = (sym-p->syms) << 8 | R_MIPS_JUMP_SLOT; 1198 } else {
1138 » » do_relocs(p, rel, sizeof rel, 2); 1199 while (i--)
1139 » } 1200 *got++ += (size_t)base;
1140 } 1201 }
1141 1202 j = 0;
1142 static void reloc_all(struct dso *p) 1203 search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1143 { 1204 i = 0;
1144 » size_t dyn[DYN_CNT]; 1205 search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1145 » for (; p; p=p->next) { 1206 Sym* sym = p->syms + j;
1146 » » if (p->relocated) continue; 1207 rel[0] = (unsigned char*)got - base;
1147 » » decode_vec(p->dynv, dyn, DYN_CNT); 1208 for (i -= j; i; i--, sym++, rel[0] += sizeof(size_t)) {
1148 » » if (NEED_MIPS_GOT_RELOCS) 1209 rel[1] = (sym - p->syms) << 8 | R_MIPS_JUMP_SLOT;
1149 » » » do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT])); 1210 do_relocs(p, rel, sizeof rel, 2);
1150 » » do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], 1211 }
1151 » » » 2+(dyn[DT_PLTREL]==DT_RELA)); 1212 }
1152 » » do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2); 1213
1153 » » do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3); 1214 static void reloc_all(struct dso* p) {
1154 1215 size_t dyn[DYN_CNT];
1155 » » if (head != &ldso && p->relro_start != p->relro_end && 1216 for (; p; p = p->next) {
1156 » » mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_sta rt, PROT_READ) 1217 if (p->relocated)
1157 » » && errno != ENOSYS) { 1218 continue;
1158 » » » error("Error relocating %s: RELRO protection failed: %m" , 1219 decode_vec(p->dynv, dyn, DYN_CNT);
1159 » » » » p->name); 1220 if (NEED_MIPS_GOT_RELOCS)
1160 » » » if (runtime) longjmp(*rtld_fail, 1); 1221 do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT]));
1161 » » } 1222 do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
1162 1223 2 + (dyn[DT_PLTREL] == DT_RELA));
1163 » » p->relocated = 1; 1224 do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
1164 » } 1225 do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
1165 } 1226
1166 1227 if (head != &ldso && p->relro_start != p->relro_end &&
1167 static void kernel_mapped_dso(struct dso *p) 1228 mprotect(laddr(p, p->relro_start), p->relro_end - p->relro_start,
1168 { 1229 PROT_READ) &&
1169 » size_t min_addr = -1, max_addr = 0, cnt; 1230 errno != ENOSYS) {
1170 » Phdr *ph = p->phdr; 1231 error("Error relocating %s: RELRO protection failed: %m", p->name);
1171 » for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) { 1232 if (runtime)
1172 » » if (ph->p_type == PT_DYNAMIC) { 1233 longjmp(*rtld_fail, 1);
1173 » » » p->dynv = laddr(p, ph->p_vaddr); 1234 }
1174 » » } else if (ph->p_type == PT_GNU_RELRO) { 1235
1175 » » » p->relro_start = ph->p_vaddr & -PAGE_SIZE; 1236 p->relocated = 1;
1176 » » » p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; 1237 }
1177 » » } 1238 }
1178 » » if (ph->p_type != PT_LOAD) continue; 1239
1179 » » if (ph->p_vaddr < min_addr) 1240 static void kernel_mapped_dso(struct dso* p) {
1180 » » » min_addr = ph->p_vaddr; 1241 size_t min_addr = -1, max_addr = 0, cnt;
1181 » » if (ph->p_vaddr+ph->p_memsz > max_addr) 1242 Phdr* ph = p->phdr;
1182 » » » max_addr = ph->p_vaddr+ph->p_memsz; 1243 for (cnt = p->phnum; cnt--; ph = (void*)((char*)ph + p->phentsize)) {
1183 » } 1244 if (ph->p_type == PT_DYNAMIC) {
1184 » min_addr &= -PAGE_SIZE; 1245 p->dynv = laddr(p, ph->p_vaddr);
1185 » max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE; 1246 } else if (ph->p_type == PT_GNU_RELRO) {
1186 » p->map = p->base + min_addr; 1247 p->relro_start = ph->p_vaddr & -PAGE_SIZE;
1187 » p->map_len = max_addr - min_addr; 1248 p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
1188 » p->kernel_mapped = 1; 1249 }
1189 } 1250 if (ph->p_type != PT_LOAD)
1190 1251 continue;
1191 void __libc_exit_fini() 1252 if (ph->p_vaddr < min_addr)
1192 { 1253 min_addr = ph->p_vaddr;
1193 » struct dso *p; 1254 if (ph->p_vaddr + ph->p_memsz > max_addr)
1194 » size_t dyn[DYN_CNT]; 1255 max_addr = ph->p_vaddr + ph->p_memsz;
1195 » for (p=fini_head; p; p=p->fini_next) { 1256 }
1196 » » if (!p->constructed) continue; 1257 min_addr &= -PAGE_SIZE;
1197 » » decode_vec(p->dynv, dyn, DYN_CNT); 1258 max_addr = (max_addr + PAGE_SIZE - 1) & -PAGE_SIZE;
1198 » » if (dyn[0] & (1<<DT_FINI_ARRAY)) { 1259 p->map = p->base + min_addr;
1199 » » » size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t); 1260 p->map_len = max_addr - min_addr;
1200 » » » size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY])+n; 1261 p->kernel_mapped = 1;
1201 » » » while (n--) ((void (*)(void))*--fn)(); 1262 }
1202 » » } 1263
1264 void __libc_exit_fini() {
1265 struct dso* p;
1266 size_t dyn[DYN_CNT];
1267 for (p = fini_head; p; p = p->fini_next) {
1268 if (!p->constructed)
1269 continue;
1270 decode_vec(p->dynv, dyn, DYN_CNT);
1271 if (dyn[0] & (1 << DT_FINI_ARRAY)) {
1272 size_t n = dyn[DT_FINI_ARRAYSZ] / sizeof(size_t);
1273 size_t* fn = (size_t*)laddr(p, dyn[DT_FINI_ARRAY]) + n;
1274 while (n--)
1275 ((void (*)(void)) * --fn)();
1276 }
1203 #ifndef NO_LEGACY_INITFINI 1277 #ifndef NO_LEGACY_INITFINI
1204 » » if ((dyn[0] & (1<<DT_FINI)) && dyn[DT_FINI]) 1278 if ((dyn[0] & (1 << DT_FINI)) && dyn[DT_FINI])
1205 » » » fpaddr(p, dyn[DT_FINI])(); 1279 fpaddr(p, dyn[DT_FINI])();
1206 #endif 1280 #endif
1207 » } 1281 }
1208 } 1282 }
1209 1283
1210 static void do_init_fini(struct dso *p) 1284 static void do_init_fini(struct dso* p) {
1211 { 1285 size_t dyn[DYN_CNT];
1212 » size_t dyn[DYN_CNT]; 1286 int need_locking = libc.threads_minus_1;
1213 » int need_locking = libc.threads_minus_1; 1287 /* Allow recursive calls that arise when a library calls
1214 » /* Allow recursive calls that arise when a library calls 1288 * dlopen from one of its constructors, but block any
1215 » * dlopen from one of its constructors, but block any 1289 * other threads until all ctors have finished. */
1216 » * other threads until all ctors have finished. */ 1290 if (need_locking)
1217 » if (need_locking) pthread_mutex_lock(&init_fini_lock); 1291 pthread_mutex_lock(&init_fini_lock);
1218 » for (; p; p=p->prev) { 1292 for (; p; p = p->prev) {
1219 » » if (p->constructed) continue; 1293 if (p->constructed)
1220 » » p->constructed = 1; 1294 continue;
1221 » » decode_vec(p->dynv, dyn, DYN_CNT); 1295 p->constructed = 1;
1222 » » if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) { 1296 decode_vec(p->dynv, dyn, DYN_CNT);
1223 » » » p->fini_next = fini_head; 1297 if (dyn[0] & ((1 << DT_FINI) | (1 << DT_FINI_ARRAY))) {
1224 » » » fini_head = p; 1298 p->fini_next = fini_head;
1225 » » } 1299 fini_head = p;
1300 }
1226 #ifndef NO_LEGACY_INITFINI 1301 #ifndef NO_LEGACY_INITFINI
1227 » » if ((dyn[0] & (1<<DT_INIT)) && dyn[DT_INIT]) 1302 if ((dyn[0] & (1 << DT_INIT)) && dyn[DT_INIT])
1228 » » » fpaddr(p, dyn[DT_INIT])(); 1303 fpaddr(p, dyn[DT_INIT])();
1229 #endif 1304 #endif
1230 » » if (dyn[0] & (1<<DT_INIT_ARRAY)) { 1305 if (dyn[0] & (1 << DT_INIT_ARRAY)) {
1231 » » » size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t); 1306 size_t n = dyn[DT_INIT_ARRAYSZ] / sizeof(size_t);
1232 » » » size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]); 1307 size_t* fn = laddr(p, dyn[DT_INIT_ARRAY]);
1233 » » » while (n--) ((void (*)(void))*fn++)(); 1308 while (n--)
1234 » » } 1309 ((void (*)(void)) * fn++)();
1235 » » if (!need_locking && libc.threads_minus_1) { 1310 }
1236 » » » need_locking = 1; 1311 if (!need_locking && libc.threads_minus_1) {
1237 » » » pthread_mutex_lock(&init_fini_lock); 1312 need_locking = 1;
1238 » » } 1313 pthread_mutex_lock(&init_fini_lock);
1239 » } 1314 }
1240 » if (need_locking) pthread_mutex_unlock(&init_fini_lock); 1315 }
1241 } 1316 if (need_locking)
1242 1317 pthread_mutex_unlock(&init_fini_lock);
1243 void __libc_start_init(void) 1318 }
1244 { 1319
1245 » do_init_fini(tail); 1320 void __libc_start_init(void) {
1246 } 1321 do_init_fini(tail);
1247 1322 }
1248 static void dl_debug_state(void) 1323
1249 { 1324 static void dl_debug_state(void) {}
1250 }
1251 1325
1252 weak_alias(dl_debug_state, _dl_debug_state); 1326 weak_alias(dl_debug_state, _dl_debug_state);
1253 1327
1254 void __init_tls(size_t *auxv) 1328 void __init_tls(size_t* auxv) {}
1255 { 1329
1256 } 1330 __attribute__((__visibility__("hidden"))) void* __tls_get_new(size_t* v) {
1257 1331 pthread_t self = __pthread_self();
1258 __attribute__((__visibility__("hidden"))) 1332
1259 void *__tls_get_new(size_t *v) 1333 /* Block signals to make accessing new TLS async-signal-safe */
1260 { 1334 sigset_t set;
1261 » pthread_t self = __pthread_self(); 1335 __block_all_sigs(&set);
1262 1336 if (v[0] <= (size_t)self->dtv[0]) {
1263 » /* Block signals to make accessing new TLS async-signal-safe */ 1337 __restore_sigs(&set);
1264 » sigset_t set; 1338 return (char*)self->dtv[v[0]] + v[1] + DTP_OFFSET;
1265 » __block_all_sigs(&set); 1339 }
1266 » if (v[0]<=(size_t)self->dtv[0]) { 1340
1267 » » __restore_sigs(&set); 1341 /* This is safe without any locks held because, if the caller
1268 » » return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET; 1342 * is able to request the Nth entry of the DTV, the DSO list
1269 » } 1343 * must be valid at least that far out and it was synchronized
1270 1344 * at program startup or by an already-completed call to dlopen. */
1271 » /* This is safe without any locks held because, if the caller 1345 struct dso* p;
1272 » * is able to request the Nth entry of the DTV, the DSO list 1346 for (p = head; p->tls_id != v[0]; p = p->next)
1273 » * must be valid at least that far out and it was synchronized 1347 ;
1274 » * at program startup or by an already-completed call to dlopen. */ 1348
1275 » struct dso *p; 1349 /* Get new DTV space from new DSO if needed */
1276 » for (p=head; p->tls_id != v[0]; p=p->next); 1350 if (v[0] > (size_t)self->dtv[0]) {
1277 1351 void** newdtv = p->new_dtv + (v[0] + 1) * a_fetch_add(&p->new_dtv_idx, 1);
1278 » /* Get new DTV space from new DSO if needed */ 1352 memcpy(newdtv, self->dtv, ((size_t)self->dtv[0] + 1) * sizeof(void*));
1279 » if (v[0] > (size_t)self->dtv[0]) { 1353 newdtv[0] = (void*)v[0];
1280 » » void **newdtv = p->new_dtv + 1354 self->dtv = self->dtv_copy = newdtv;
1281 » » » (v[0]+1)*a_fetch_add(&p->new_dtv_idx,1); 1355 }
1282 » » memcpy(newdtv, self->dtv, 1356
1283 » » » ((size_t)self->dtv[0]+1) * sizeof(void *)); 1357 /* Get new TLS memory from all new DSOs up to the requested one */
1284 » » newdtv[0] = (void *)v[0]; 1358 unsigned char* mem;
1285 » » self->dtv = self->dtv_copy = newdtv; 1359 for (p = head;; p = p->next) {
1286 » } 1360 if (!p->tls_id || self->dtv[p->tls_id])
1287 1361 continue;
1288 » /* Get new TLS memory from all new DSOs up to the requested one */ 1362 mem = p->new_tls +
1289 » unsigned char *mem; 1363 (p->tls.size + p->tls.align) * a_fetch_add(&p->new_tls_idx, 1);
1290 » for (p=head; ; p=p->next) { 1364 mem += ((uintptr_t)p->tls.image - (uintptr_t)mem) & (p->tls.align - 1);
1291 » » if (!p->tls_id || self->dtv[p->tls_id]) continue; 1365 self->dtv[p->tls_id] = mem;
1292 » » mem = p->new_tls + (p->tls.size + p->tls.align) 1366 memcpy(mem, p->tls.image, p->tls.len);
1293 » » » * a_fetch_add(&p->new_tls_idx,1); 1367 if (p->tls_id == v[0])
1294 » » mem += ((uintptr_t)p->tls.image - (uintptr_t)mem) 1368 break;
1295 » » » & (p->tls.align-1); 1369 }
1296 » » self->dtv[p->tls_id] = mem; 1370 __restore_sigs(&set);
1297 » » memcpy(mem, p->tls.image, p->tls.len); 1371 return mem + v[1] + DTP_OFFSET;
1298 » » if (p->tls_id == v[0]) break; 1372 }
1299 » } 1373
1300 » __restore_sigs(&set); 1374 static void update_tls_size() {
1301 » return mem + v[1] + DTP_OFFSET; 1375 libc.tls_cnt = tls_cnt;
1302 } 1376 libc.tls_align = tls_align;
1303 1377 libc.tls_size = ALIGN((1 + tls_cnt) * sizeof(void*) + tls_offset +
1304 static void update_tls_size() 1378 sizeof(struct pthread) + tls_align * 2,
1305 { 1379 tls_align);
1306 » libc.tls_cnt = tls_cnt;
1307 » libc.tls_align = tls_align;
1308 » libc.tls_size = ALIGN(
1309 » » (1+tls_cnt) * sizeof(void *) +
1310 » » tls_offset +
1311 » » sizeof(struct pthread) +
1312 » » tls_align * 2,
1313 » tls_align);
1314 } 1380 }
1315 1381
1316 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the 1382 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1317 * following stage 2 and stage 3 functions via primitive symbolic lookup 1383 * following stage 2 and stage 3 functions via primitive symbolic lookup
1318 * since it does not have access to their addresses to begin with. */ 1384 * since it does not have access to their addresses to begin with. */
1319 1385
1320 /* Stage 2 of the dynamic linker is called after relative relocations 1386 /* Stage 2 of the dynamic linker is called after relative relocations
1321 * have been processed. It can make function calls to static functions 1387 * have been processed. It can make function calls to static functions
1322 * and access string literals and static data, but cannot use extern 1388 * and access string literals and static data, but cannot use extern
1323 * symbols. Its job is to perform symbolic relocations on the dynamic 1389 * symbols. Its job is to perform symbolic relocations on the dynamic
1324 * linker itself, but some of the relocations performed may need to be 1390 * linker itself, but some of the relocations performed may need to be
1325 * replaced later due to copy relocations in the main program. */ 1391 * replaced later due to copy relocations in the main program. */
1326 1392
1327 __attribute__((__visibility__("hidden"))) 1393 __attribute__((__visibility__("hidden"))) void __dls2(unsigned char* base,
1328 void __dls2(unsigned char *base, size_t *sp) 1394 size_t* sp) {
1329 { 1395 if (DL_FDPIC) {
1330 » if (DL_FDPIC) { 1396 void* p1 = (void*)sp[-2];
1331 » » void *p1 = (void *)sp[-2]; 1397 void* p2 = (void*)sp[-1];
1332 » » void *p2 = (void *)sp[-1]; 1398 if (!p1) {
1333 » » if (!p1) { 1399 size_t *auxv, aux[AUX_CNT];
1334 » » » size_t *auxv, aux[AUX_CNT]; 1400 for (auxv = sp + 1 + *sp + 1; *auxv; auxv++)
1335 » » » for (auxv=sp+1+*sp+1; *auxv; auxv++) 1401 ; // Pass
1336 » » » » ; // Pass 1402 auxv++;
1337 » » » auxv++; 1403 decode_vec(auxv, aux, AUX_CNT);
1338 » » » decode_vec(auxv, aux, AUX_CNT); 1404 if (aux[AT_BASE])
1339 » » » if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE]; 1405 ldso.base = (void*)aux[AT_BASE];
1340 » » » else ldso.base = (void *)(aux[AT_PHDR] & -4096); 1406 else
1341 » » } 1407 ldso.base = (void*)(aux[AT_PHDR] & -4096);
1342 » » app_loadmap = p2 ? p1 : 0; 1408 }
1343 » » ldso.loadmap = p2 ? p2 : p1; 1409 app_loadmap = p2 ? p1 : 0;
1344 » » ldso.base = laddr(&ldso, 0); 1410 ldso.loadmap = p2 ? p2 : p1;
1345 » } else { 1411 ldso.base = laddr(&ldso, 0);
1346 » » ldso.base = base; 1412 } else {
1347 » } 1413 ldso.base = base;
1348 » Ehdr *ehdr = (void *)ldso.base; 1414 }
1349 » ldso.name = ldso.shortname = "libc.so"; 1415 Ehdr* ehdr = (void*)ldso.base;
1350 » ldso.global = 1; 1416 ldso.name = ldso.shortname = "libc.so";
1351 » ldso.phnum = ehdr->e_phnum; 1417 ldso.global = 1;
1352 » ldso.phdr = laddr(&ldso, ehdr->e_phoff); 1418 ldso.phnum = ehdr->e_phnum;
1353 » ldso.phentsize = ehdr->e_phentsize; 1419 ldso.phdr = laddr(&ldso, ehdr->e_phoff);
1354 » kernel_mapped_dso(&ldso); 1420 ldso.phentsize = ehdr->e_phentsize;
1355 » decode_dyn(&ldso); 1421 kernel_mapped_dso(&ldso);
1356 1422 decode_dyn(&ldso);
1357 » if (DL_FDPIC) makefuncdescs(&ldso); 1423
1358 1424 if (DL_FDPIC)
1359 » /* Prepare storage for to save clobbered REL addends so they 1425 makefuncdescs(&ldso);
1360 » * can be reused in stage 3. There should be very few. If 1426
1361 » * something goes wrong and there are a huge number, abort 1427 /* Prepare storage for to save clobbered REL addends so they
1362 » * instead of risking stack overflow. */ 1428 * can be reused in stage 3. There should be very few. If
1363 » size_t dyn[DYN_CNT]; 1429 * something goes wrong and there are a huge number, abort
1364 » decode_vec(ldso.dynv, dyn, DYN_CNT); 1430 * instead of risking stack overflow. */
1365 » size_t *rel = laddr(&ldso, dyn[DT_REL]); 1431 size_t dyn[DYN_CNT];
1366 » size_t rel_size = dyn[DT_RELSZ]; 1432 decode_vec(ldso.dynv, dyn, DYN_CNT);
1367 » size_t symbolic_rel_cnt = 0; 1433 size_t* rel = laddr(&ldso, dyn[DT_REL]);
1368 » apply_addends_to = rel; 1434 size_t rel_size = dyn[DT_RELSZ];
1369 » for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) 1435 size_t symbolic_rel_cnt = 0;
1370 » » if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++; 1436 apply_addends_to = rel;
1371 » if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash(); 1437 for (; rel_size; rel += 2, rel_size -= 2 * sizeof(size_t))
1372 » size_t addends[symbolic_rel_cnt+1]; 1438 if (!IS_RELATIVE(rel[1], ldso.syms))
1373 » saved_addends = addends; 1439 symbolic_rel_cnt++;
1374 1440 if (symbolic_rel_cnt >= ADDEND_LIMIT)
1375 » head = &ldso; 1441 a_crash();
1376 » reloc_all(&ldso); 1442 size_t addends[symbolic_rel_cnt + 1];
1377 1443 saved_addends = addends;
1378 » ldso.relocated = 0; 1444
1379 1445 head = &ldso;
1380 » /* Call dynamic linker stage-3, __dls3, looking it up 1446 reloc_all(&ldso);
1381 » * symbolically as a barrier against moving the address 1447
1382 » * load across the above relocation processing. */ 1448 ldso.relocated = 0;
1383 » struct symdef dls3_def = find_sym(&ldso, "__dls3", 0); 1449
1384 » if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp) ; 1450 /* Call dynamic linker stage-3, __dls3, looking it up
1385 » else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp); 1451 * symbolically as a barrier against moving the address
1452 * load across the above relocation processing. */
1453 struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
1454 if (DL_FDPIC)
1455 ((stage3_func)&ldso.funcdescs[dls3_def.sym - ldso.syms])(sp);
1456 else
1457 ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp);
1386 } 1458 }
1387 1459
1388 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc 1460 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1389 * fully functional. Its job is to load (if not already loaded) and 1461 * fully functional. Its job is to load (if not already loaded) and
1390 * process dependencies and relocations for the main application and 1462 * process dependencies and relocations for the main application and
1391 * transfer control to its entry point. */ 1463 * transfer control to its entry point. */
1392 1464
1393 _Noreturn void __dls3(size_t *sp) 1465 _Noreturn void __dls3(size_t* sp) {
1394 { 1466 static struct dso app, vdso;
1395 » static struct dso app, vdso; 1467 size_t aux[AUX_CNT], *auxv;
1396 » size_t aux[AUX_CNT], *auxv; 1468 size_t i;
1397 » size_t i; 1469 char* env_preload = 0;
1398 » char *env_preload=0; 1470 size_t vdso_base;
1399 » size_t vdso_base; 1471 int argc = *sp;
1400 » int argc = *sp; 1472 char** argv = (void*)(sp + 1);
1401 » char **argv = (void *)(sp+1); 1473 char** argv_orig = argv;
1402 » char **argv_orig = argv; 1474 char** envp = argv + argc + 1;
1403 » char **envp = argv+argc+1; 1475
1404 1476 /* Find aux vector just past environ[] and use it to initialize
1405 » /* Find aux vector just past environ[] and use it to initialize 1477 * global data that may be needed before we can make syscalls. */
1406 » * global data that may be needed before we can make syscalls. */ 1478 __environ = envp;
1407 » __environ = envp; 1479 for (i = argc + 1; argv[i]; i++)
1408 » for (i=argc+1; argv[i]; i++); 1480 ;
1409 » libc.auxv = auxv = (void *)(argv+i+1); 1481 libc.auxv = auxv = (void*)(argv + i + 1);
1410 » decode_vec(auxv, aux, AUX_CNT); 1482 decode_vec(auxv, aux, AUX_CNT);
1411 » __hwcap = aux[AT_HWCAP]; 1483 __hwcap = aux[AT_HWCAP];
1412 » libc.page_size = aux[AT_PAGESZ]; 1484 libc.page_size = aux[AT_PAGESZ];
1413 » libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] 1485 libc.secure = ((aux[0] & 0x7800) != 0x7800 || aux[AT_UID] != aux[AT_EUID] ||
1414 » » || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]); 1486 aux[AT_GID] != aux[AT_EGID] || aux[AT_SECURE]);
1415 1487
1416 » /* Setup early thread pointer in builtin_tls for ldso/libc itself to 1488 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1417 » * use during dynamic linking. If possible it will also serve as the 1489 * use during dynamic linking. If possible it will also serve as the
1418 » * thread pointer at runtime. */ 1490 * thread pointer at runtime. */
1419 » libc.tls_size = sizeof builtin_tls; 1491 libc.tls_size = sizeof builtin_tls;
1420 » libc.tls_align = tls_align; 1492 libc.tls_align = tls_align;
1421 » if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) { 1493 if (__init_tp(__copy_tls((void*)builtin_tls)) < 0) {
1422 » » a_crash(); 1494 a_crash();
1423 » } 1495 }
1424 1496
1425 » /* Only trust user/env if kernel says we're not suid/sgid */ 1497 /* Only trust user/env if kernel says we're not suid/sgid */
1426 » if (!libc.secure) { 1498 if (!libc.secure) {
1427 » » env_path = getenv("LD_LIBRARY_PATH"); 1499 env_path = getenv("LD_LIBRARY_PATH");
1428 » » env_preload = getenv("LD_PRELOAD"); 1500 env_preload = getenv("LD_PRELOAD");
1429 » } 1501 }
1430 1502
1431 » /* If the main program was already loaded by the kernel, 1503 /* If the main program was already loaded by the kernel,
1432 » * AT_PHDR will point to some location other than the dynamic 1504 * AT_PHDR will point to some location other than the dynamic
1433 » * linker's program headers. */ 1505 * linker's program headers. */
1434 » if (aux[AT_PHDR] != (size_t)ldso.phdr) { 1506 if (aux[AT_PHDR] != (size_t)ldso.phdr) {
1435 » » size_t interp_off = 0; 1507 size_t interp_off = 0;
1436 » » size_t tls_image = 0; 1508 size_t tls_image = 0;
1437 » » /* Find load address of the main program, via AT_PHDR vs PT_PHDR . */ 1509 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1438 » » Phdr *phdr = app.phdr = (void *)aux[AT_PHDR]; 1510 Phdr* phdr = app.phdr = (void*)aux[AT_PHDR];
1439 » » app.phnum = aux[AT_PHNUM]; 1511 app.phnum = aux[AT_PHNUM];
1440 » » app.phentsize = aux[AT_PHENT]; 1512 app.phentsize = aux[AT_PHENT];
1441 » » for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[A T_PHENT])) { 1513 for (i = aux[AT_PHNUM]; i;
1442 » » » if (phdr->p_type == PT_PHDR) 1514 i--, phdr = (void*)((char*)phdr + aux[AT_PHENT])) {
1443 » » » » app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr ); 1515 if (phdr->p_type == PT_PHDR)
1444 » » » else if (phdr->p_type == PT_INTERP) 1516 app.base = (void*)(aux[AT_PHDR] - phdr->p_vaddr);
1445 » » » » interp_off = (size_t)phdr->p_vaddr; 1517 else if (phdr->p_type == PT_INTERP)
1446 » » » else if (phdr->p_type == PT_TLS) { 1518 interp_off = (size_t)phdr->p_vaddr;
1447 » » » » tls_image = phdr->p_vaddr; 1519 else if (phdr->p_type == PT_TLS) {
1448 » » » » app.tls.len = phdr->p_filesz; 1520 tls_image = phdr->p_vaddr;
1449 » » » » app.tls.size = phdr->p_memsz; 1521 app.tls.len = phdr->p_filesz;
1450 » » » » app.tls.align = phdr->p_align; 1522 app.tls.size = phdr->p_memsz;
1451 » » » } 1523 app.tls.align = phdr->p_align;
1452 » » } 1524 }
1453 » » if (DL_FDPIC) app.loadmap = app_loadmap; 1525 }
1454 » » if (app.tls.size) app.tls.image = laddr(&app, tls_image); 1526 if (DL_FDPIC)
1455 » » if (interp_off) ldso.name = laddr(&app, interp_off); 1527 app.loadmap = app_loadmap;
1456 » » if ((aux[0] & (1UL<<AT_EXECFN)) 1528 if (app.tls.size)
1457 » » && strncmp((char *)aux[AT_EXECFN], "/proc/", 6)) 1529 app.tls.image = laddr(&app, tls_image);
1458 » » » app.name = (char *)aux[AT_EXECFN]; 1530 if (interp_off)
1459 » » else 1531 ldso.name = laddr(&app, interp_off);
1460 » » » app.name = argv[0]; 1532 if ((aux[0] & (1UL << AT_EXECFN)) &&
1461 » » kernel_mapped_dso(&app); 1533 strncmp((char*)aux[AT_EXECFN], "/proc/", 6))
1462 » } else { 1534 app.name = (char*)aux[AT_EXECFN];
1463 » » int fd; 1535 else
1464 » » char *ldname = argv[0]; 1536 app.name = argv[0];
1465 » » size_t l = strlen(ldname); 1537 kernel_mapped_dso(&app);
1466 » » if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1; 1538 } else {
1467 » » argv++; 1539 int fd;
1468 » » while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') { 1540 char* ldname = argv[0];
1469 » » » char *opt = argv[0]+2; 1541 size_t l = strlen(ldname);
1470 » » » *argv++ = (void *)-1; 1542 if (l >= 3 && !strcmp(ldname + l - 3, "ldd"))
1471 » » » if (!*opt) { 1543 ldd_mode = 1;
1472 » » » » break; 1544 argv++;
1473 » » » } else if (!memcmp(opt, "list", 5)) { 1545 while (argv[0] && argv[0][0] == '-' && argv[0][1] == '-') {
1474 » » » » ldd_mode = 1; 1546 char* opt = argv[0] + 2;
1475 » » » } else if (!memcmp(opt, "library-path", 12)) { 1547 *argv++ = (void*)-1;
1476 » » » » if (opt[12]=='=') env_path = opt+13; 1548 if (!*opt) {
1477 » » » » else if (opt[12]) *argv = 0; 1549 break;
1478 » » » » else if (*argv) env_path = *argv++; 1550 } else if (!memcmp(opt, "list", 5)) {
1479 » » » } else if (!memcmp(opt, "preload", 7)) { 1551 ldd_mode = 1;
1480 » » » » if (opt[7]=='=') env_preload = opt+8; 1552 } else if (!memcmp(opt, "library-path", 12)) {
1481 » » » » else if (opt[7]) *argv = 0; 1553 if (opt[12] == '=')
1482 » » » » else if (*argv) env_preload = *argv++; 1554 env_path = opt + 13;
1483 » » » } else { 1555 else if (opt[12])
1484 » » » » argv[0] = 0; 1556 *argv = 0;
1485 » » » } 1557 else if (*argv)
1486 » » } 1558 env_path = *argv++;
1487 » » argv[-1] = (void *)(argc - (argv-argv_orig)); 1559 } else if (!memcmp(opt, "preload", 7)) {
1488 » » if (!argv[0]) { 1560 if (opt[7] == '=')
1489 » » » dprintf(2, "musl libc (" LDSO_ARCH ")\n" 1561 env_preload = opt + 8;
1490 » » » » "Version %s\n" 1562 else if (opt[7])
1491 » » » » "Dynamic Program Loader\n" 1563 *argv = 0;
1492 » » » » "Usage: %s [options] [--] pathname%s\n", 1564 else if (*argv)
1493 » » » » __libc_get_version(), ldname, 1565 env_preload = *argv++;
1494 » » » » ldd_mode ? "" : " [args]"); 1566 } else {
1495 » » » _exit(1); 1567 argv[0] = 0;
1496 » » } 1568 }
1497 » » fd = open(argv[0], O_RDONLY); 1569 }
1498 » » if (fd < 0) { 1570 argv[-1] = (void*)(argc - (argv - argv_orig));
1499 » » » dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno)); 1571 if (!argv[0]) {
1500 » » » _exit(1); 1572 dprintf(2, "musl libc (" LDSO_ARCH
1501 » » } 1573 ")\n"
1502 » » runtime = 1; 1574 "Version %s\n"
1503 » » Ehdr *ehdr = (void *)map_library(fd, &app); 1575 "Dynamic Program Loader\n"
1504 » » if (!ehdr) { 1576 "Usage: %s [options] [--] pathname%s\n",
1505 » » » dprintf(2, "%s: %s: Not a valid dynamic program\n", ldna me, argv[0]); 1577 __libc_get_version(), ldname, ldd_mode ? "" : " [args]");
1506 » » » _exit(1); 1578 _exit(1);
1507 » » } 1579 }
1508 » » runtime = 0; 1580 fd = open(argv[0], O_RDONLY);
1509 » » close(fd); 1581 if (fd < 0) {
1510 » » ldso.name = ldname; 1582 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
1511 » » app.name = argv[0]; 1583 _exit(1);
1512 » » aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry); 1584 }
1513 » » /* Find the name that would have been used for the dynamic 1585 runtime = 1;
1514 » » * linker had ldd not taken its place. */ 1586 Ehdr* ehdr = (void*)map_library(fd, &app);
1515 » » if (ldd_mode) { 1587 if (!ehdr) {
1516 » » » for (i=0; i<app.phnum; i++) { 1588 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
1517 » » » » if (app.phdr[i].p_type == PT_INTERP) 1589 _exit(1);
1518 » » » » » ldso.name = laddr(&app, app.phdr[i].p_va ddr); 1590 }
1519 » » » } 1591 runtime = 0;
1520 » » » dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base); 1592 close(fd);
1521 » » } 1593 ldso.name = ldname;
1522 » } 1594 app.name = argv[0];
1523 » if (app.tls.size) { 1595 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry);
1524 » » libc.tls_head = tls_tail = &app.tls; 1596 /* Find the name that would have been used for the dynamic
1525 » » app.tls_id = tls_cnt = 1; 1597 * linker had ldd not taken its place. */
1598 if (ldd_mode) {
1599 for (i = 0; i < app.phnum; i++) {
1600 if (app.phdr[i].p_type == PT_INTERP)
1601 ldso.name = laddr(&app, app.phdr[i].p_vaddr);
1602 }
1603 dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base);
1604 }
1605 }
1606 if (app.tls.size) {
1607 libc.tls_head = tls_tail = &app.tls;
1608 app.tls_id = tls_cnt = 1;
1526 #ifdef TLS_ABOVE_TP 1609 #ifdef TLS_ABOVE_TP
1527 » » app.tls.offset = 0; 1610 app.tls.offset = 0;
1528 » » tls_offset = app.tls.size 1611 tls_offset = app.tls.size + (-((uintptr_t)app.tls.image + app.tls.size) &
1529 » » » + ( -((uintptr_t)app.tls.image + app.tls.size) 1612 (app.tls.align - 1));
1530 » » » & (app.tls.align-1) );
1531 #else 1613 #else
1532 » » tls_offset = app.tls.offset = app.tls.size 1614 tls_offset = app.tls.offset =
1533 » » » + ( -((uintptr_t)app.tls.image + app.tls.size) 1615 app.tls.size +
1534 » » » & (app.tls.align-1) ); 1616 (-((uintptr_t)app.tls.image + app.tls.size) & (app.tls.align - 1));
1535 #endif 1617 #endif
1536 tls_align = MAXP2(tls_align, app.tls.align); 1618 tls_align = MAXP2(tls_align, app.tls.align);
1537 } 1619 }
1538 app.global = 1; 1620 app.global = 1;
1539 decode_dyn(&app); 1621 decode_dyn(&app);
1540 if (DL_FDPIC) { 1622 if (DL_FDPIC) {
1541 makefuncdescs(&app); 1623 makefuncdescs(&app);
1542 if (!app.loadmap) { 1624 if (!app.loadmap) {
1543 app.loadmap = (void *)&app_dummy_loadmap; 1625 app.loadmap = (void*)&app_dummy_loadmap;
1544 app.loadmap->nsegs = 1; 1626 app.loadmap->nsegs = 1;
1545 app.loadmap->segs[0].addr = (size_t)app.map; 1627 app.loadmap->segs[0].addr = (size_t)app.map;
1546 app.loadmap->segs[0].p_vaddr = (size_t)app.map 1628 app.loadmap->segs[0].p_vaddr = (size_t)app.map - (size_t)app.base;
1547 - (size_t)app.base; 1629 app.loadmap->segs[0].p_memsz = app.map_len;
1548 app.loadmap->segs[0].p_memsz = app.map_len; 1630 }
1549 } 1631 argv[-3] = (void*)app.loadmap;
1550 argv[-3] = (void *)app.loadmap; 1632 }
1551 } 1633
1552 1634 /* Attach to vdso, if provided by the kernel */
1553 /* Attach to vdso, if provided by the kernel */ 1635 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
1554 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) { 1636 Ehdr* ehdr = (void*)vdso_base;
1555 Ehdr *ehdr = (void *)vdso_base; 1637 Phdr* phdr = vdso.phdr = (void*)(vdso_base + ehdr->e_phoff);
1556 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff); 1638 vdso.phnum = ehdr->e_phnum;
1557 vdso.phnum = ehdr->e_phnum; 1639 vdso.phentsize = ehdr->e_phentsize;
1558 vdso.phentsize = ehdr->e_phentsize; 1640 for (i = ehdr->e_phnum; i;
1559 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr- >e_phentsize)) { 1641 i--, phdr = (void*)((char*)phdr + ehdr->e_phentsize)) {
1560 if (phdr->p_type == PT_DYNAMIC) 1642 if (phdr->p_type == PT_DYNAMIC)
1561 vdso.dynv = (void *)(vdso_base + phdr->p_offset) ; 1643 vdso.dynv = (void*)(vdso_base + phdr->p_offset);
1562 if (phdr->p_type == PT_LOAD) 1644 if (phdr->p_type == PT_LOAD)
1563 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset); 1645 vdso.base = (void*)(vdso_base - phdr->p_vaddr + phdr->p_offset);
1564 } 1646 }
1565 vdso.name = ""; 1647 vdso.name = "";
1566 vdso.shortname = "linux-gate.so.1"; 1648 vdso.shortname = "linux-gate.so.1";
1567 vdso.global = 1; 1649 vdso.global = 1;
1568 vdso.relocated = 1; 1650 vdso.relocated = 1;
1569 decode_dyn(&vdso); 1651 decode_dyn(&vdso);
1570 vdso.prev = &ldso; 1652 vdso.prev = &ldso;
1571 ldso.next = &vdso; 1653 ldso.next = &vdso;
1572 } 1654 }
1573 1655
1574 /* Initial dso chain consists only of the app. */ 1656 /* Initial dso chain consists only of the app. */
1575 head = tail = &app; 1657 head = tail = &app;
1576 1658
1577 /* Donate unused parts of app and library mapping to malloc */ 1659 /* Donate unused parts of app and library mapping to malloc */
1578 reclaim_gaps(&app); 1660 reclaim_gaps(&app);
1579 reclaim_gaps(&ldso); 1661 reclaim_gaps(&ldso);
1580 1662
1581 /* Load preload/needed libraries, add their symbols to the global 1663 /* Load preload/needed libraries, add their symbols to the global
1582 * namespace, and perform all remaining relocations. */ 1664 * namespace, and perform all remaining relocations. */
1583 if (env_preload) load_preload(env_preload); 1665 if (env_preload)
1584 load_deps(&app); 1666 load_preload(env_preload);
1585 make_global(&app); 1667 load_deps(&app);
1586 1668 make_global(&app);
1587 for (i=0; app.dynv[i]; i+=2) { 1669
1588 if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG) 1670 for (i = 0; app.dynv[i]; i += 2) {
1589 app.dynv[i+1] = (size_t)&debug; 1671 if (!DT_DEBUG_INDIRECT && app.dynv[i] == DT_DEBUG)
1590 if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) { 1672 app.dynv[i + 1] = (size_t)&debug;
1591 size_t *ptr = (size_t *) app.dynv[i+1]; 1673 if (DT_DEBUG_INDIRECT && app.dynv[i] == DT_DEBUG_INDIRECT) {
1592 *ptr = (size_t)&debug; 1674 size_t* ptr = (size_t*)app.dynv[i + 1];
1593 } 1675 *ptr = (size_t)&debug;
1594 } 1676 }
1595 1677 }
1596 /* The main program must be relocated LAST since it may contin 1678
1597 * copy relocations which depend on libraries' relocations. */ 1679 /* The main program must be relocated LAST since it may contin
1598 reloc_all(app.next); 1680 * copy relocations which depend on libraries' relocations. */
1599 reloc_all(&app); 1681 reloc_all(app.next);
1600 1682 reloc_all(&app);
1601 update_tls_size(); 1683
1602 if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) { 1684 update_tls_size();
1603 void *initial_tls = calloc(libc.tls_size, 1); 1685 if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) {
1604 if (!initial_tls) { 1686 void* initial_tls = calloc(libc.tls_size, 1);
1605 dprintf(2, "%s: Error getting %zu bytes thread-local sto rage: %m\n", 1687 if (!initial_tls) {
1606 argv[0], libc.tls_size); 1688 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
1607 _exit(127); 1689 argv[0], libc.tls_size);
1608 } 1690 _exit(127);
1609 if (__init_tp(__copy_tls(initial_tls)) < 0) { 1691 }
1610 a_crash(); 1692 if (__init_tp(__copy_tls(initial_tls)) < 0) {
1611 } 1693 a_crash();
1612 } else { 1694 }
1613 size_t tmp_tls_size = libc.tls_size; 1695 } else {
1614 pthread_t self = __pthread_self(); 1696 size_t tmp_tls_size = libc.tls_size;
1615 /* Temporarily set the tls size to the full size of 1697 pthread_t self = __pthread_self();
1616 * builtin_tls so that __copy_tls will use the same layout 1698 /* Temporarily set the tls size to the full size of
1617 * as it did for before. Then check, just to be safe. */ 1699 * builtin_tls so that __copy_tls will use the same layout
1618 libc.tls_size = sizeof builtin_tls; 1700 * as it did for before. Then check, just to be safe. */
1619 if (__copy_tls((void*)builtin_tls) != self) a_crash(); 1701 libc.tls_size = sizeof builtin_tls;
1620 libc.tls_size = tmp_tls_size; 1702 if (__copy_tls((void*)builtin_tls) != self)
1621 } 1703 a_crash();
1622 static_tls_cnt = tls_cnt; 1704 libc.tls_size = tmp_tls_size;
1623 1705 }
1624 if (ldso_fail) _exit(127); 1706 static_tls_cnt = tls_cnt;
1625 if (ldd_mode) _exit(0); 1707
1626 1708 if (ldso_fail)
1627 /* Switch to runtime mode: any further failures in the dynamic 1709 _exit(127);
1628 * linker are a reportable failure rather than a fatal startup 1710 if (ldd_mode)
1629 * error. */ 1711 _exit(0);
1630 runtime = 1; 1712
1631 1713 /* Switch to runtime mode: any further failures in the dynamic
1632 debug.ver = 1; 1714 * linker are a reportable failure rather than a fatal startup
1633 debug.bp = dl_debug_state; 1715 * error. */
1634 debug.head = head; 1716 runtime = 1;
1635 debug.base = ldso.base; 1717
1636 debug.state = 0; 1718 debug.ver = 1;
1637 _dl_debug_state(); 1719 debug.bp = dl_debug_state;
1638 1720 debug.head = head;
1639 errno = 0; 1721 debug.base = ldso.base;
1640 1722 debug.state = 0;
1641 CRTJMP((void *)aux[AT_ENTRY], argv-1); 1723 _dl_debug_state();
1642 for(;;); 1724
1643 } 1725 errno = 0;
1644 1726
1645 void *dlopen(const char *file, int mode) 1727 CRTJMP((void*)aux[AT_ENTRY], argv - 1);
1646 { 1728 for (;;)
1647 struct dso *volatile p, *orig_tail, *next; 1729 ;
1648 struct tls_module *orig_tls_tail; 1730 }
1649 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align; 1731
1650 size_t i; 1732 void* dlopen(const char* file, int mode) {
1651 int cs; 1733 struct dso* volatile p, *orig_tail, *next;
1652 jmp_buf jb; 1734 struct tls_module* orig_tls_tail;
1653 1735 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
1654 if (!file) return head; 1736 size_t i;
1655 1737 int cs;
1656 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); 1738 jmp_buf jb;
1657 pthread_rwlock_wrlock(&lock); 1739
1658 __inhibit_ptc(); 1740 if (!file)
1659 1741 return head;
1660 p = 0; 1742
1661 orig_tls_tail = tls_tail; 1743 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
1662 orig_tls_cnt = tls_cnt; 1744 pthread_rwlock_wrlock(&lock);
1663 orig_tls_offset = tls_offset; 1745 __inhibit_ptc();
1664 orig_tls_align = tls_align; 1746
1665 orig_tail = tail; 1747 p = 0;
1666 noload = mode & RTLD_NOLOAD; 1748 orig_tls_tail = tls_tail;
1667 1749 orig_tls_cnt = tls_cnt;
1668 rtld_fail = &jb; 1750 orig_tls_offset = tls_offset;
1669 if (setjmp(*rtld_fail)) { 1751 orig_tls_align = tls_align;
1670 /* Clean up anything new that was (partially) loaded */ 1752 orig_tail = tail;
1671 if (p && p->deps) for (i=0; p->deps[i]; i++) 1753 noload = mode & RTLD_NOLOAD;
1672 if (p->deps[i]->global < 0) 1754
1673 p->deps[i]->global = 0; 1755 rtld_fail = &jb;
1674 for (p=orig_tail->next; p; p=next) { 1756 if (setjmp(*rtld_fail)) {
1675 next = p->next; 1757 /* Clean up anything new that was (partially) loaded */
1676 while (p->td_index) { 1758 if (p && p->deps)
1677 void *tmp = p->td_index->next; 1759 for (i = 0; p->deps[i]; i++)
1678 free(p->td_index); 1760 if (p->deps[i]->global < 0)
1679 p->td_index = tmp; 1761 p->deps[i]->global = 0;
1680 } 1762 for (p = orig_tail->next; p; p = next) {
1681 free(p->funcdescs); 1763 next = p->next;
1682 if (p->rpath != p->rpath_orig) 1764 while (p->td_index) {
1683 free(p->rpath); 1765 void* tmp = p->td_index->next;
1684 free(p->deps); 1766 free(p->td_index);
1685 unmap_library(p); 1767 p->td_index = tmp;
1686 free(p); 1768 }
1687 } 1769 free(p->funcdescs);
1688 if (!orig_tls_tail) libc.tls_head = 0; 1770 if (p->rpath != p->rpath_orig)
1689 tls_tail = orig_tls_tail; 1771 free(p->rpath);
1690 tls_cnt = orig_tls_cnt; 1772 free(p->deps);
1691 tls_offset = orig_tls_offset; 1773 unmap_library(p);
1692 tls_align = orig_tls_align; 1774 free(p);
1693 tail = orig_tail; 1775 }
1694 tail->next = 0; 1776 if (!orig_tls_tail)
1695 p = 0; 1777 libc.tls_head = 0;
1696 goto end; 1778 tls_tail = orig_tls_tail;
1697 } else p = load_library(file, head); 1779 tls_cnt = orig_tls_cnt;
1698 1780 tls_offset = orig_tls_offset;
1699 if (!p) { 1781 tls_align = orig_tls_align;
1700 error(noload ? 1782 tail = orig_tail;
1701 "Library %s is not already loaded" : 1783 tail->next = 0;
1702 "Error loading shared library %s: %m", 1784 p = 0;
1703 file); 1785 goto end;
1704 goto end; 1786 } else
1705 } 1787 p = load_library(file, head);
1706 1788
1707 /* First load handling */ 1789 if (!p) {
1708 if (!p->deps) { 1790 error(noload ? "Library %s is not already loaded"
1709 load_deps(p); 1791 : "Error loading shared library %s: %m",
1710 if (p->deps) for (i=0; p->deps[i]; i++) 1792 file);
1711 if (!p->deps[i]->global) 1793 goto end;
1712 p->deps[i]->global = -1; 1794 }
1713 if (!p->global) p->global = -1; 1795
1714 reloc_all(p); 1796 /* First load handling */
1715 if (p->deps) for (i=0; p->deps[i]; i++) 1797 if (!p->deps) {
1716 if (p->deps[i]->global < 0) 1798 load_deps(p);
1717 p->deps[i]->global = 0; 1799 if (p->deps)
1718 if (p->global < 0) p->global = 0; 1800 for (i = 0; p->deps[i]; i++)
1719 } 1801 if (!p->deps[i]->global)
1720 1802 p->deps[i]->global = -1;
1721 if (mode & RTLD_GLOBAL) { 1803 if (!p->global)
1722 if (p->deps) for (i=0; p->deps[i]; i++) 1804 p->global = -1;
1723 p->deps[i]->global = 1; 1805 reloc_all(p);
1724 p->global = 1; 1806 if (p->deps)
1725 } 1807 for (i = 0; p->deps[i]; i++)
1726 1808 if (p->deps[i]->global < 0)
1727 update_tls_size(); 1809 p->deps[i]->global = 0;
1728 _dl_debug_state(); 1810 if (p->global < 0)
1729 orig_tail = tail; 1811 p->global = 0;
1812 }
1813
1814 if (mode & RTLD_GLOBAL) {
1815 if (p->deps)
1816 for (i = 0; p->deps[i]; i++)
1817 p->deps[i]->global = 1;
1818 p->global = 1;
1819 }
1820
1821 update_tls_size();
1822 _dl_debug_state();
1823 orig_tail = tail;
1730 end: 1824 end:
1731 » __release_ptc(); 1825 __release_ptc();
1732 » if (p) gencnt++; 1826 if (p)
1733 » pthread_rwlock_unlock(&lock); 1827 gencnt++;
1734 » if (p) do_init_fini(orig_tail); 1828 pthread_rwlock_unlock(&lock);
1735 » pthread_setcancelstate(cs, 0); 1829 if (p)
1736 » return p; 1830 do_init_fini(orig_tail);
1737 } 1831 pthread_setcancelstate(cs, 0);
1738 1832 return p;
1739 __attribute__((__visibility__("hidden"))) 1833 }
1740 int __dl_invalid_handle(void *h) 1834
1741 { 1835 __attribute__((__visibility__("hidden"))) int __dl_invalid_handle(void* h) {
1742 » struct dso *p; 1836 struct dso* p;
1743 » for (p=head; p; p=p->next) if (h==p) return 0; 1837 for (p = head; p; p = p->next)
1744 » error("Invalid library handle %p", (void *)h); 1838 if (h == p)
1745 » return 1; 1839 return 0;
1746 } 1840 error("Invalid library handle %p", (void*)h);
1747 1841 return 1;
1748 static void *addr2dso(size_t a) 1842 }
1749 { 1843
1750 » struct dso *p; 1844 static void* addr2dso(size_t a) {
1751 » size_t i; 1845 struct dso* p;
1752 » if (DL_FDPIC) for (p=head; p; p=p->next) { 1846 size_t i;
1753 » » i = count_syms(p); 1847 if (DL_FDPIC)
1754 » » if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs)) 1848 for (p = head; p; p = p->next) {
1755 » » » return p; 1849 i = count_syms(p);
1756 » } 1850 if (a - (size_t)p->funcdescs < i * sizeof(*p->funcdescs))
1757 » for (p=head; p; p=p->next) { 1851 return p;
1758 » » if (DL_FDPIC && p->loadmap) { 1852 }
1759 » » » for (i=0; i<p->loadmap->nsegs; i++) { 1853 for (p = head; p; p = p->next) {
1760 » » » » if (a-p->loadmap->segs[i].p_vaddr 1854 if (DL_FDPIC && p->loadmap) {
1761 » » » » < p->loadmap->segs[i].p_memsz) 1855 for (i = 0; i < p->loadmap->nsegs; i++) {
1762 » » » » » return p; 1856 if (a - p->loadmap->segs[i].p_vaddr < p->loadmap->segs[i].p_memsz)
1763 » » » } 1857 return p;
1764 » » } else { 1858 }
1765 » » » if (a-(size_t)p->map < p->map_len) 1859 } else {
1766 » » » » return p; 1860 if (a - (size_t)p->map < p->map_len)
1767 » » } 1861 return p;
1768 » } 1862 }
1769 » return 0; 1863 }
1770 } 1864 return 0;
1771 1865 }
1772 void *__tls_get_addr(size_t *); 1866
1773 1867 void* __tls_get_addr(size_t*);
1774 static void *do_dlsym(struct dso *p, const char *s, void *ra) 1868
1775 { 1869 static void* do_dlsym(struct dso* p, const char* s, void* ra) {
1776 » size_t i; 1870 size_t i;
1777 » uint32_t h = 0, gh = 0, *ght; 1871 uint32_t h = 0, gh = 0, *ght;
1778 » Sym *sym; 1872 Sym* sym;
1779 » if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) { 1873 if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
1780 » » if (p == RTLD_DEFAULT) { 1874 if (p == RTLD_DEFAULT) {
1781 » » » p = head; 1875 p = head;
1782 » » } else if (p == RTLD_NEXT) { 1876 } else if (p == RTLD_NEXT) {
1783 » » » p = addr2dso((size_t)ra); 1877 p = addr2dso((size_t)ra);
1784 » » » if (!p) p=head; 1878 if (!p)
1785 » » » p = p->next; 1879 p = head;
1786 » » } 1880 p = p->next;
1787 » » struct symdef def = find_sym(p, s, 0); 1881 }
1788 » » if (!def.sym) goto failed; 1882 struct symdef def = find_sym(p, s, 0);
1789 » » if ((def.sym->st_info&0xf) == STT_TLS) 1883 if (!def.sym)
1790 » » » return __tls_get_addr((size_t []){def.dso->tls_id, def.s ym->st_value}); 1884 goto failed;
1791 » » if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC) 1885 if ((def.sym->st_info & 0xf) == STT_TLS)
1792 » » » return def.dso->funcdescs + (def.sym - def.dso->syms); 1886 return __tls_get_addr((size_t[]){def.dso->tls_id, def.sym->st_value});
1793 » » return laddr(def.dso, def.sym->st_value); 1887 if (DL_FDPIC && (def.sym->st_info & 0xf) == STT_FUNC)
1794 » } 1888 return def.dso->funcdescs + (def.sym - def.dso->syms);
1795 » if (__dl_invalid_handle(p)) 1889 return laddr(def.dso, def.sym->st_value);
1796 » » return 0; 1890 }
1797 » if ((ght = p->ghashtab)) { 1891 if (__dl_invalid_handle(p))
1798 » » gh = gnu_hash(s); 1892 return 0;
1799 » » sym = gnu_lookup(gh, ght, p, s); 1893 if ((ght = p->ghashtab)) {
1800 » } else { 1894 gh = gnu_hash(s);
1801 » » h = sysv_hash(s); 1895 sym = gnu_lookup(gh, ght, p, s);
1802 » » sym = sysv_lookup(s, h, p); 1896 } else {
1803 » } 1897 h = sysv_hash(s);
1804 » if (sym && (sym->st_info&0xf) == STT_TLS) 1898 sym = sysv_lookup(s, h, p);
1805 » » return __tls_get_addr((size_t []){p->tls_id, sym->st_value}); 1899 }
1806 » if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == STT_FUNC) 1900 if (sym && (sym->st_info & 0xf) == STT_TLS)
1807 » » return p->funcdescs + (sym - p->syms); 1901 return __tls_get_addr((size_t[]){p->tls_id, sym->st_value});
1808 » if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) 1902 if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info & 0xf) == STT_FUNC)
1809 » » return laddr(p, sym->st_value); 1903 return p->funcdescs + (sym - p->syms);
1810 » if (p->deps) for (i=0; p->deps[i]; i++) { 1904 if (sym && sym->st_value && (1 << (sym->st_info & 0xf) & OK_TYPES))
1811 » » if ((ght = p->deps[i]->ghashtab)) { 1905 return laddr(p, sym->st_value);
1812 » » » if (!gh) gh = gnu_hash(s); 1906 if (p->deps)
1813 » » » sym = gnu_lookup(gh, ght, p->deps[i], s); 1907 for (i = 0; p->deps[i]; i++) {
1814 » » } else { 1908 if ((ght = p->deps[i]->ghashtab)) {
1815 » » » if (!h) h = sysv_hash(s); 1909 if (!gh)
1816 » » » sym = sysv_lookup(s, h, p->deps[i]); 1910 gh = gnu_hash(s);
1817 » » } 1911 sym = gnu_lookup(gh, ght, p->deps[i], s);
1818 » » if (sym && (sym->st_info&0xf) == STT_TLS) 1912 } else {
1819 » » » return __tls_get_addr((size_t []){p->deps[i]->tls_id, sy m->st_value}); 1913 if (!h)
1820 » » if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == ST T_FUNC) 1914 h = sysv_hash(s);
1821 » » » return p->deps[i]->funcdescs + (sym - p->deps[i]->syms); 1915 sym = sysv_lookup(s, h, p->deps[i]);
1822 » » if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) 1916 }
1823 » » » return laddr(p->deps[i], sym->st_value); 1917 if (sym && (sym->st_info & 0xf) == STT_TLS)
1824 » } 1918 return __tls_get_addr((size_t[]){p->deps[i]->tls_id, sym->st_value});
1919 if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info & 0xf) == STT_FUNC)
1920 return p->deps[i]->funcdescs + (sym - p->deps[i]->syms);
1921 if (sym && sym->st_value && (1 << (sym->st_info & 0xf) & OK_TYPES))
1922 return laddr(p->deps[i], sym->st_value);
1923 }
1825 failed: 1924 failed:
1826 » error("Symbol not found: %s", s); 1925 error("Symbol not found: %s", s);
1827 » return 0; 1926 return 0;
1828 } 1927 }
1829 1928
1830 int dladdr(const void *addr, Dl_info *info) 1929 int dladdr(const void* addr, Dl_info* info) {
1831 { 1930 struct dso* p;
1832 » struct dso *p; 1931 Sym *sym, *bestsym;
1833 » Sym *sym, *bestsym; 1932 uint32_t nsym;
1834 » uint32_t nsym; 1933 char* strings;
1835 » char *strings; 1934 void* best = 0;
1836 » void *best = 0; 1935
1837 1936 pthread_rwlock_rdlock(&lock);
1838 » pthread_rwlock_rdlock(&lock); 1937 p = addr2dso((size_t)addr);
1839 » p = addr2dso((size_t)addr); 1938 pthread_rwlock_unlock(&lock);
1840 » pthread_rwlock_unlock(&lock); 1939
1841 1940 if (!p)
1842 » if (!p) return 0; 1941 return 0;
1843 1942
1844 » sym = p->syms; 1943 sym = p->syms;
1845 » strings = p->strings; 1944 strings = p->strings;
1846 » nsym = count_syms(p); 1945 nsym = count_syms(p);
1847 1946
1848 » if (DL_FDPIC) { 1947 if (DL_FDPIC) {
1849 » » size_t idx = ((size_t)addr-(size_t)p->funcdescs) 1948 size_t idx = ((size_t)addr - (size_t)p->funcdescs) / sizeof(*p->funcdescs);
1850 » » » / sizeof(*p->funcdescs); 1949 if (idx < nsym && (sym[idx].st_info & 0xf) == STT_FUNC) {
1851 » » if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) { 1950 best = p->funcdescs + idx;
1852 » » » best = p->funcdescs + idx; 1951 bestsym = sym + idx;
1853 » » » bestsym = sym + idx; 1952 }
1854 » » } 1953 }
1855 » } 1954
1856 1955 if (!best)
1857 » if (!best) for (; nsym; nsym--, sym++) { 1956 for (; nsym; nsym--, sym++) {
1858 » » if (sym->st_value 1957 if (sym->st_value && (1 << (sym->st_info & 0xf) & OK_TYPES) &&
1859 » » && (1<<(sym->st_info&0xf) & OK_TYPES) 1958 (1 << (sym->st_info >> 4) & OK_BINDS)) {
1860 » » && (1<<(sym->st_info>>4) & OK_BINDS)) { 1959 void* symaddr = laddr(p, sym->st_value);
1861 » » » void *symaddr = laddr(p, sym->st_value); 1960 if (symaddr > addr || symaddr < best)
1862 » » » if (symaddr > addr || symaddr < best) 1961 continue;
1863 » » » » continue; 1962 best = symaddr;
1864 » » » best = symaddr; 1963 bestsym = sym;
1865 » » » bestsym = sym; 1964 if (addr == symaddr)
1866 » » » if (addr == symaddr) 1965 break;
1867 » » » » break; 1966 }
1868 » » } 1967 }
1869 » } 1968
1870 1969 if (!best)
1871 » if (!best) return 0; 1970 return 0;
1872 1971
1873 » if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC) 1972 if (DL_FDPIC && (bestsym->st_info & 0xf) == STT_FUNC)
1874 » » best = p->funcdescs + (bestsym - p->syms); 1973 best = p->funcdescs + (bestsym - p->syms);
1875 1974
1876 » info->dli_fname = p->name; 1975 info->dli_fname = p->name;
1877 » info->dli_fbase = p->base; 1976 info->dli_fbase = p->base;
1878 » info->dli_sname = strings + bestsym->st_name; 1977 info->dli_sname = strings + bestsym->st_name;
1879 » info->dli_saddr = best; 1978 info->dli_saddr = best;
1880 1979
1881 » return 1; 1980 return 1;
1882 } 1981 }
1883 1982
1884 __attribute__((__visibility__("hidden"))) 1983 __attribute__((__visibility__("hidden"))) void* __dlsym(void* restrict p,
1885 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra) 1984 const char* restrict s,
1886 { 1985 void* restrict ra) {
1887 » void *res; 1986 void* res;
1888 » pthread_rwlock_rdlock(&lock); 1987 pthread_rwlock_rdlock(&lock);
1889 » res = do_dlsym(p, s, ra); 1988 res = do_dlsym(p, s, ra);
1890 » pthread_rwlock_unlock(&lock); 1989 pthread_rwlock_unlock(&lock);
1891 » return res; 1990 return res;
1892 } 1991 }
1893 1992
1894 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data) 1993 int dl_iterate_phdr(int (*callback)(struct dl_phdr_info* info,
1895 { 1994 size_t size,
1896 » struct dso *current; 1995 void* data),
1897 » struct dl_phdr_info info; 1996 void* data) {
1898 » int ret = 0; 1997 struct dso* current;
1899 » for(current = head; current;) { 1998 struct dl_phdr_info info;
1900 » » info.dlpi_addr = (uintptr_t)current->base; 1999 int ret = 0;
1901 » » info.dlpi_name = current->name; 2000 for (current = head; current;) {
1902 » » info.dlpi_phdr = current->phdr; 2001 info.dlpi_addr = (uintptr_t)current->base;
1903 » » info.dlpi_phnum = current->phnum; 2002 info.dlpi_name = current->name;
1904 » » info.dlpi_adds = gencnt; 2003 info.dlpi_phdr = current->phdr;
1905 » » info.dlpi_subs = 0; 2004 info.dlpi_phnum = current->phnum;
1906 » » info.dlpi_tls_modid = current->tls_id; 2005 info.dlpi_adds = gencnt;
1907 » » info.dlpi_tls_data = current->tls.image; 2006 info.dlpi_subs = 0;
1908 2007 info.dlpi_tls_modid = current->tls_id;
1909 » » ret = (callback)(&info, sizeof (info), data); 2008 info.dlpi_tls_data = current->tls.image;
1910 2009
1911 » » if (ret != 0) break; 2010 ret = (callback)(&info, sizeof(info), data);
1912 2011
1913 » » pthread_rwlock_rdlock(&lock); 2012 if (ret != 0)
1914 » » current = current->next; 2013 break;
1915 » » pthread_rwlock_unlock(&lock); 2014
1916 » } 2015 pthread_rwlock_rdlock(&lock);
1917 » return ret; 2016 current = current->next;
1918 } 2017 pthread_rwlock_unlock(&lock);
1919 2018 }
1920 __attribute__((__visibility__("hidden"))) 2019 return ret;
1921 void __dl_vseterr(const char *, va_list); 2020 }
1922 2021
1923 static void error(const char *fmt, ...) 2022 __attribute__((__visibility__("hidden"))) void __dl_vseterr(const char*,
1924 { 2023 va_list);
1925 » va_list ap; 2024
1926 » va_start(ap, fmt); 2025 static void error(const char* fmt, ...) {
1927 » if (!runtime) { 2026 va_list ap;
1928 » » vdprintf(2, fmt, ap); 2027 va_start(ap, fmt);
1929 » » dprintf(2, "\n"); 2028 if (!runtime) {
1930 » » ldso_fail = 1; 2029 vdprintf(2, fmt, ap);
1931 » » va_end(ap); 2030 dprintf(2, "\n");
1932 » » return; 2031 ldso_fail = 1;
1933 » } 2032 va_end(ap);
1934 » __dl_vseterr(fmt, ap); 2033 return;
1935 » va_end(ap); 2034 }
1936 } 2035 __dl_vseterr(fmt, ap);
2036 va_end(ap);
2037 }
OLDNEW
« fusl/arch/aarch64/atomic_arch.h ('K') | « fusl/ldso/dlstart.c ('k') | fusl/src/aio/aio.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698