Rich Felker | f419bcb | 2012-08-26 21:09:26 -0400 | [diff] [blame] | 1 | #define _GNU_SOURCE |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdint.h> |
| 7 | #include <elf.h> |
| 8 | #include <sys/mman.h> |
| 9 | #include <limits.h> |
| 10 | #include <stdint.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <errno.h> |
| 14 | #include <limits.h> |
| 15 | #include <elf.h> |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 16 | #include <link.h> |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 17 | #include <setjmp.h> |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 18 | #include <pthread.h> |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 19 | #include <ctype.h> |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 20 | #include <dlfcn.h> |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 21 | #include "pthread_impl.h" |
| 22 | #include "libc.h" |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 23 | |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 24 | static int errflag; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 25 | static char errbuf[128]; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 26 | |
Rich Felker | e864a29 | 2012-07-11 01:47:30 -0400 | [diff] [blame] | 27 | #ifdef SHARED |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 28 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 29 | #if ULONG_MAX == 0xffffffff |
| 30 | typedef Elf32_Ehdr Ehdr; |
| 31 | typedef Elf32_Phdr Phdr; |
| 32 | typedef Elf32_Sym Sym; |
| 33 | #define R_TYPE(x) ((x)&255) |
| 34 | #define R_SYM(x) ((x)>>8) |
| 35 | #else |
| 36 | typedef Elf64_Ehdr Ehdr; |
| 37 | typedef Elf64_Phdr Phdr; |
| 38 | typedef Elf64_Sym Sym; |
| 39 | #define R_TYPE(x) ((x)&0xffffffff) |
| 40 | #define R_SYM(x) ((x)>>32) |
| 41 | #endif |
| 42 | |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 43 | #define MAXP2(a,b) (-(-(a)&-(b))) |
| 44 | #define ALIGN(x,y) ((x)+(y)-1 & -(y)) |
| 45 | |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 46 | struct debug { |
| 47 | int ver; |
| 48 | void *head; |
| 49 | void (*bp)(void); |
| 50 | int state; |
| 51 | void *base; |
| 52 | }; |
| 53 | |
| 54 | struct dso { |
| 55 | unsigned char *base; |
| 56 | char *name; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 57 | size_t *dynv; |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 58 | struct dso *next, *prev; |
| 59 | |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 60 | Phdr *phdr; |
| 61 | int phnum; |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 62 | int refcnt; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 63 | Sym *syms; |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 64 | uint32_t *hashtab; |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 65 | uint32_t *ghashtab; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 66 | char *strings; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 67 | unsigned char *map; |
| 68 | size_t map_len; |
| 69 | dev_t dev; |
| 70 | ino_t ino; |
Rich Felker | 6343ac8 | 2012-06-09 21:20:44 -0400 | [diff] [blame] | 71 | signed char global; |
Rich Felker | 700a815 | 2012-02-07 20:29:29 -0500 | [diff] [blame] | 72 | char relocated; |
| 73 | char constructed; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 74 | struct dso **deps; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 75 | void *tls_image; |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 76 | size_t tls_len, tls_size, tls_align, tls_id, tls_offset; |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 77 | void **new_dtv; |
| 78 | unsigned char *new_tls; |
| 79 | int new_dtv_idx, new_tls_idx; |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 80 | struct dso *fini_next; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 81 | char *shortname; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 82 | char buf[]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 85 | struct symdef { |
| 86 | Sym *sym; |
| 87 | struct dso *dso; |
| 88 | }; |
| 89 | |
Rich Felker | 59f4086 | 2012-08-05 13:46:39 -0400 | [diff] [blame] | 90 | #include "reloc.h" |
| 91 | |
Rich Felker | 58aa5f4 | 2012-05-03 20:42:45 -0400 | [diff] [blame] | 92 | void __init_ssp(size_t *); |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 93 | void *__install_initial_tls(void *); |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame] | 94 | |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 95 | static struct dso *head, *tail, *ldso, *fini_head; |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 96 | static char *env_path, *sys_path, *r_path; |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 97 | static unsigned long long gencnt; |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame] | 98 | static int ssp_used; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 99 | static int runtime; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 100 | static int ldd_mode; |
Rich Felker | 0410950 | 2012-08-18 16:00:23 -0400 | [diff] [blame] | 101 | static int ldso_fail; |
Rich Felker | 4d07e55 | 2013-01-23 22:07:45 -0500 | [diff] [blame] | 102 | static int noload; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 103 | static jmp_buf rtld_fail; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 104 | static pthread_rwlock_t lock; |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 105 | static struct debug debug; |
Rich Felker | c62b9f3 | 2012-10-14 19:56:50 -0400 | [diff] [blame] | 106 | static size_t tls_cnt, tls_offset, tls_align = 4*sizeof(size_t); |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 107 | static pthread_mutex_t init_fini_lock = { ._m_type = PTHREAD_MUTEX_RECURSIVE }; |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 108 | |
| 109 | struct debug *_dl_debug_addr = &debug; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 110 | |
Rich Felker | 0a96a37 | 2012-10-07 21:43:46 -0400 | [diff] [blame] | 111 | #define AUX_CNT 38 |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 112 | #define DYN_CNT 34 |
| 113 | |
| 114 | static void decode_vec(size_t *v, size_t *a, size_t cnt) |
| 115 | { |
| 116 | memset(a, 0, cnt*sizeof(size_t)); |
| 117 | for (; v[0]; v+=2) if (v[0]<cnt) { |
| 118 | a[0] |= 1ULL<<v[0]; |
| 119 | a[v[0]] = v[1]; |
| 120 | } |
| 121 | } |
| 122 | |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 123 | static int search_vec(size_t *v, size_t *r, size_t key) |
| 124 | { |
| 125 | for (; v[0]!=key; v+=2) |
| 126 | if (!v[0]) return 0; |
| 127 | *r = v[1]; |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | static uint32_t sysv_hash(const char *s0) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 132 | { |
Rich Felker | 2adf2fb | 2012-01-17 00:34:58 -0500 | [diff] [blame] | 133 | const unsigned char *s = (void *)s0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 134 | uint_fast32_t h = 0; |
| 135 | while (*s) { |
| 136 | h = 16*h + *s++; |
| 137 | h ^= h>>24 & 0xf0; |
| 138 | } |
| 139 | return h & 0xfffffff; |
| 140 | } |
| 141 | |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 142 | static uint32_t gnu_hash(const char *s0) |
| 143 | { |
| 144 | const unsigned char *s = (void *)s0; |
| 145 | uint_fast32_t h = 5381; |
| 146 | for (; *s; s++) |
| 147 | h = h*33 + *s; |
| 148 | return h; |
| 149 | } |
| 150 | |
| 151 | static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 152 | { |
| 153 | size_t i; |
Rich Felker | 05eff01 | 2012-08-05 02:38:35 -0400 | [diff] [blame] | 154 | Sym *syms = dso->syms; |
| 155 | uint32_t *hashtab = dso->hashtab; |
| 156 | char *strings = dso->strings; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 157 | for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { |
| 158 | if (!strcmp(s, strings+syms[i].st_name)) |
| 159 | return syms+i; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 164 | static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso) |
| 165 | { |
| 166 | Sym *sym; |
| 167 | char *strings; |
| 168 | uint32_t *hashtab = dso->ghashtab; |
| 169 | uint32_t nbuckets = hashtab[0]; |
| 170 | uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4); |
| 171 | uint32_t h2; |
| 172 | uint32_t *hashval; |
| 173 | uint32_t n = buckets[h1 % nbuckets]; |
| 174 | |
| 175 | if (!n) return 0; |
| 176 | |
| 177 | strings = dso->strings; |
| 178 | sym = dso->syms + n; |
| 179 | hashval = buckets + nbuckets + (n - hashtab[1]); |
| 180 | |
| 181 | for (h1 |= 1; ; sym++) { |
| 182 | h2 = *hashval++; |
| 183 | if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name)) |
| 184 | return sym; |
| 185 | if (h2 & 1) break; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 191 | #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS) |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 192 | #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 193 | |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 194 | static struct symdef find_sym(struct dso *dso, const char *s, int need_def) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 195 | { |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 196 | uint32_t h = 0, gh = 0; |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 197 | struct symdef def = {0}; |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 198 | if (dso->ghashtab) { |
| 199 | gh = gnu_hash(s); |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 200 | if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; |
| 201 | } else { |
| 202 | h = sysv_hash(s); |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 203 | if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; |
| 204 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 205 | for (; dso; dso=dso->next) { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 206 | Sym *sym; |
| 207 | if (!dso->global) continue; |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 208 | if (dso->ghashtab) { |
| 209 | if (!gh) gh = gnu_hash(s); |
| 210 | sym = gnu_lookup(s, gh, dso); |
| 211 | } else { |
| 212 | if (!h) h = sysv_hash(s); |
| 213 | sym = sysv_lookup(s, h, dso); |
| 214 | } |
Rich Felker | bd17431 | 2012-10-06 01:36:11 -0400 | [diff] [blame] | 215 | if (!sym) continue; |
| 216 | if (!sym->st_shndx) |
| 217 | if (need_def || (sym->st_info&0xf) == STT_TLS) |
| 218 | continue; |
| 219 | if (!sym->st_value) |
| 220 | if ((sym->st_info&0xf) != STT_TLS) |
| 221 | continue; |
| 222 | if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue; |
| 223 | if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue; |
| 224 | |
| 225 | if (def.sym && sym->st_info>>4 == STB_WEAK) continue; |
| 226 | def.sym = sym; |
| 227 | def.dso = dso; |
| 228 | if (sym->st_info>>4 == STB_GLOBAL) break; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 229 | } |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 230 | return def; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Rich Felker | 87d13a4 | 2012-08-05 02:49:02 -0400 | [diff] [blame] | 233 | static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 234 | { |
Rich Felker | 87d13a4 | 2012-08-05 02:49:02 -0400 | [diff] [blame] | 235 | unsigned char *base = dso->base; |
| 236 | Sym *syms = dso->syms; |
| 237 | char *strings = dso->strings; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 238 | Sym *sym; |
| 239 | const char *name; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 240 | void *ctx; |
| 241 | int type; |
| 242 | int sym_index; |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 243 | struct symdef def; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 244 | |
| 245 | for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 246 | type = R_TYPE(rel[1]); |
| 247 | sym_index = R_SYM(rel[1]); |
| 248 | if (sym_index) { |
| 249 | sym = syms + sym_index; |
| 250 | name = strings + sym->st_name; |
Rich Felker | 7cb44cd | 2012-08-05 02:44:32 -0400 | [diff] [blame] | 251 | ctx = IS_COPY(type) ? head->next : head; |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 252 | def = find_sym(ctx, name, IS_PLT(type)); |
| 253 | if (!def.sym && sym->st_info>>4 != STB_WEAK) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 254 | snprintf(errbuf, sizeof errbuf, |
| 255 | "Error relocating %s: %s: symbol not found", |
Rich Felker | 87d13a4 | 2012-08-05 02:49:02 -0400 | [diff] [blame] | 256 | dso->name, name); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 257 | if (runtime) longjmp(rtld_fail, 1); |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 258 | dprintf(2, "%s\n", errbuf); |
Rich Felker | 0410950 | 2012-08-18 16:00:23 -0400 | [diff] [blame] | 259 | ldso_fail = 1; |
| 260 | continue; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 261 | } |
Rich Felker | 7d9a5c6 | 2012-08-05 14:03:17 -0400 | [diff] [blame] | 262 | } else { |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 263 | sym = 0; |
| 264 | def.sym = 0; |
| 265 | def.dso = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 266 | } |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 267 | do_single_reloc(dso, base, (void *)(base + rel[0]), type, |
| 268 | stride>2 ? rel[2] : 0, sym, sym?sym->st_size:0, def, |
| 269 | def.sym?(size_t)(def.dso->base+def.sym->st_value):0); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 273 | /* A huge hack: to make up for the wastefulness of shared libraries |
| 274 | * needing at least a page of dirty memory even if they have no global |
| 275 | * data, we reclaim the gaps at the beginning and end of writable maps |
| 276 | * and "donate" them to the heap by setting up minimal malloc |
| 277 | * structures and then freeing them. */ |
| 278 | |
| 279 | static void reclaim(unsigned char *base, size_t start, size_t end) |
| 280 | { |
| 281 | size_t *a, *z; |
| 282 | start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t); |
| 283 | end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t); |
| 284 | if (start>end || end-start < 4*sizeof(size_t)) return; |
| 285 | a = (size_t *)(base + start); |
| 286 | z = (size_t *)(base + end); |
| 287 | a[-2] = 1; |
| 288 | a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1; |
| 289 | z[1] = 1; |
| 290 | free(a); |
| 291 | } |
| 292 | |
| 293 | static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt) |
| 294 | { |
| 295 | for (; phcnt--; ph=(void *)((char *)ph+phent)) { |
| 296 | if (ph->p_type!=PT_LOAD) continue; |
| 297 | if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue; |
| 298 | reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr); |
| 299 | reclaim(base, ph->p_vaddr+ph->p_memsz, |
| 300 | ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE); |
| 301 | } |
| 302 | } |
| 303 | |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 304 | static void *map_library(int fd, struct dso *dso) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 305 | { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 306 | Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 307 | size_t phsize; |
| 308 | size_t addr_min=SIZE_MAX, addr_max=0, map_len; |
| 309 | size_t this_min, this_max; |
| 310 | off_t off_start; |
| 311 | Ehdr *eh; |
| 312 | Phdr *ph; |
| 313 | unsigned prot; |
| 314 | unsigned char *map, *base; |
| 315 | size_t dyn; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 316 | size_t tls_image=0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 317 | size_t i; |
| 318 | |
| 319 | ssize_t l = read(fd, buf, sizeof buf); |
| 320 | if (l<sizeof *eh) return 0; |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 321 | eh = buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 322 | phsize = eh->e_phentsize * eh->e_phnum; |
| 323 | if (phsize + sizeof *eh > l) return 0; |
| 324 | if (eh->e_phoff + phsize > l) { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 325 | l = pread(fd, buf+1, phsize, eh->e_phoff); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 326 | if (l != phsize) return 0; |
| 327 | eh->e_phoff = sizeof *eh; |
| 328 | } |
| 329 | ph = (void *)((char *)buf + eh->e_phoff); |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 330 | dso->phdr = ph; |
| 331 | dso->phnum = eh->e_phnum; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 332 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 333 | if (ph->p_type == PT_DYNAMIC) |
| 334 | dyn = ph->p_vaddr; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 335 | if (ph->p_type == PT_TLS) { |
| 336 | tls_image = ph->p_vaddr; |
| 337 | dso->tls_align = ph->p_align; |
| 338 | dso->tls_len = ph->p_filesz; |
| 339 | dso->tls_size = ph->p_memsz; |
| 340 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 341 | if (ph->p_type != PT_LOAD) continue; |
| 342 | if (ph->p_vaddr < addr_min) { |
| 343 | addr_min = ph->p_vaddr; |
| 344 | off_start = ph->p_offset; |
| 345 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 346 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 347 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 348 | } |
| 349 | if (ph->p_vaddr+ph->p_memsz > addr_max) { |
| 350 | addr_max = ph->p_vaddr+ph->p_memsz; |
| 351 | } |
| 352 | } |
| 353 | if (!dyn) return 0; |
| 354 | addr_max += PAGE_SIZE-1; |
| 355 | addr_max &= -PAGE_SIZE; |
| 356 | addr_min &= -PAGE_SIZE; |
| 357 | off_start &= -PAGE_SIZE; |
| 358 | map_len = addr_max - addr_min + off_start; |
| 359 | /* The first time, we map too much, possibly even more than |
| 360 | * the length of the file. This is okay because we will not |
| 361 | * use the invalid part; we just need to reserve the right |
| 362 | * amount of virtual address space to map over later. */ |
Rich Felker | bf30100 | 2011-06-28 14:20:41 -0400 | [diff] [blame] | 363 | map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 364 | if (map==MAP_FAILED) return 0; |
| 365 | base = map - addr_min; |
| 366 | ph = (void *)((char *)buf + eh->e_phoff); |
| 367 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 368 | if (ph->p_type != PT_LOAD) continue; |
| 369 | /* Reuse the existing mapping for the lowest-address LOAD */ |
| 370 | if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue; |
| 371 | this_min = ph->p_vaddr & -PAGE_SIZE; |
| 372 | this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; |
| 373 | off_start = ph->p_offset & -PAGE_SIZE; |
| 374 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 375 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 376 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 377 | if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) |
| 378 | goto error; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 379 | if (ph->p_memsz > ph->p_filesz) { |
| 380 | size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; |
| 381 | size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; |
| 382 | memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 383 | if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) |
| 384 | goto error; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 385 | } |
| 386 | } |
Rich Felker | 9f17413 | 2011-06-29 00:29:08 -0400 | [diff] [blame] | 387 | for (i=0; ((size_t *)(base+dyn))[i]; i+=2) |
| 388 | if (((size_t *)(base+dyn))[i]==DT_TEXTREL) { |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 389 | if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0) |
| 390 | goto error; |
Rich Felker | 9f17413 | 2011-06-29 00:29:08 -0400 | [diff] [blame] | 391 | break; |
| 392 | } |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 393 | if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff), |
| 394 | eh->e_phentsize, eh->e_phnum); |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 395 | dso->map = map; |
| 396 | dso->map_len = map_len; |
| 397 | dso->base = base; |
| 398 | dso->dynv = (void *)(base+dyn); |
| 399 | if (dso->tls_size) dso->tls_image = (void *)(base+tls_image); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 400 | return map; |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 401 | error: |
| 402 | munmap(map, map_len); |
| 403 | return 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 404 | } |
| 405 | |
Rich Felker | 8c203ea | 2013-04-20 11:51:58 -0400 | [diff] [blame] | 406 | static int path_open(const char *name, const char *s, char *buf, size_t buf_size) |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 407 | { |
Rich Felker | 8c203ea | 2013-04-20 11:51:58 -0400 | [diff] [blame] | 408 | size_t l; |
| 409 | int fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 410 | for (;;) { |
Rich Felker | 8c203ea | 2013-04-20 11:51:58 -0400 | [diff] [blame] | 411 | s += strspn(s, ":\n"); |
| 412 | l = strcspn(s, ":\n"); |
| 413 | if (l-1 >= INT_MAX) return -1; |
| 414 | if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) >= buf_size) |
| 415 | continue; |
Rich Felker | f2d08cf | 2012-09-29 17:59:50 -0400 | [diff] [blame] | 416 | if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 417 | s += l; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 418 | } |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 419 | } |
| 420 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 421 | static void decode_dyn(struct dso *p) |
| 422 | { |
| 423 | size_t dyn[DYN_CNT] = {0}; |
| 424 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 425 | p->syms = (void *)(p->base + dyn[DT_SYMTAB]); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 426 | p->strings = (void *)(p->base + dyn[DT_STRTAB]); |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 427 | if (dyn[0]&(1<<DT_HASH)) |
| 428 | p->hashtab = (void *)(p->base + dyn[DT_HASH]); |
| 429 | if (search_vec(p->dynv, dyn, DT_GNU_HASH)) |
| 430 | p->ghashtab = (void *)(p->base + *dyn); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 431 | } |
| 432 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 433 | static struct dso *load_library(const char *name) |
| 434 | { |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 435 | char buf[2*NAME_MAX+2]; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 436 | const char *pathname; |
Rich Felker | 1d7c4f8 | 2012-12-15 23:34:08 -0500 | [diff] [blame] | 437 | unsigned char *map; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 438 | struct dso *p, temp_dso = {0}; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 439 | int fd; |
| 440 | struct stat st; |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 441 | size_t alloc_size; |
| 442 | int n_th = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 443 | |
| 444 | /* Catch and block attempts to reload the implementation itself */ |
| 445 | if (name[0]=='l' && name[1]=='i' && name[2]=='b') { |
| 446 | static const char *rp, reserved[] = |
| 447 | "c\0pthread\0rt\0m\0dl\0util\0xnet\0"; |
| 448 | char *z = strchr(name, '.'); |
| 449 | if (z) { |
| 450 | size_t l = z-name; |
| 451 | for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1); |
| 452 | if (*rp) { |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 453 | if (!ldso->prev) { |
| 454 | tail->next = ldso; |
| 455 | ldso->prev = tail; |
| 456 | tail = ldso->next ? ldso->next : ldso; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 457 | } |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 458 | return ldso; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 462 | if (strchr(name, '/')) { |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 463 | pathname = name; |
Rich Felker | f2d08cf | 2012-09-29 17:59:50 -0400 | [diff] [blame] | 464 | fd = open(name, O_RDONLY|O_CLOEXEC); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 465 | } else { |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 466 | /* Search for the name to see if it's already loaded */ |
| 467 | for (p=head->next; p; p=p->next) { |
| 468 | if (p->shortname && !strcmp(p->shortname, name)) { |
| 469 | p->refcnt++; |
| 470 | return p; |
| 471 | } |
| 472 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 473 | if (strlen(name) > NAME_MAX) return 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 474 | fd = -1; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 475 | if (r_path) fd = path_open(name, r_path, buf, sizeof buf); |
| 476 | if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf); |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 477 | if (fd < 0) { |
| 478 | if (!sys_path) { |
Rich Felker | d712dd3 | 2012-09-29 18:14:46 -0400 | [diff] [blame] | 479 | FILE *f = fopen(ETC_LDSO_PATH, "rbe"); |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 480 | if (f) { |
Rich Felker | 11bc173 | 2013-06-26 10:17:29 -0400 | [diff] [blame] | 481 | if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) { |
Rich Felker | 59b481d | 2013-06-26 10:51:36 -0400 | [diff] [blame] | 482 | free(sys_path); |
Rich Felker | 11bc173 | 2013-06-26 10:17:29 -0400 | [diff] [blame] | 483 | sys_path = ""; |
Rich Felker | 6546510 | 2012-11-09 13:49:40 -0500 | [diff] [blame] | 484 | } |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 485 | fclose(f); |
| 486 | } |
| 487 | } |
Rich Felker | 40d5f7e | 2012-11-08 22:41:16 -0500 | [diff] [blame] | 488 | if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib"; |
| 489 | fd = path_open(name, sys_path, buf, sizeof buf); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 490 | } |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 491 | pathname = buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 492 | } |
| 493 | if (fd < 0) return 0; |
| 494 | if (fstat(fd, &st) < 0) { |
| 495 | close(fd); |
| 496 | return 0; |
| 497 | } |
| 498 | for (p=head->next; p; p=p->next) { |
| 499 | if (p->dev == st.st_dev && p->ino == st.st_ino) { |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 500 | /* If this library was previously loaded with a |
| 501 | * pathname but a search found the same inode, |
| 502 | * setup its shortname so it can be found by name. */ |
Rich Felker | 5f88c0e | 2012-10-05 12:09:54 -0400 | [diff] [blame] | 503 | if (!p->shortname && pathname != name) |
| 504 | p->shortname = strrchr(p->name, '/')+1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 505 | close(fd); |
| 506 | p->refcnt++; |
| 507 | return p; |
| 508 | } |
| 509 | } |
Rich Felker | 4d07e55 | 2013-01-23 22:07:45 -0500 | [diff] [blame] | 510 | map = noload ? 0 : map_library(fd, &temp_dso); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 511 | close(fd); |
| 512 | if (!map) return 0; |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 513 | |
| 514 | /* Allocate storage for the new DSO. When there is TLS, this |
| 515 | * storage must include a reservation for all pre-existing |
| 516 | * threads to obtain copies of both the new TLS, and an |
| 517 | * extended DTV capable of storing an additional slot for |
| 518 | * the newly-loaded DSO. */ |
| 519 | alloc_size = sizeof *p + strlen(pathname) + 1; |
| 520 | if (runtime && temp_dso.tls_image) { |
| 521 | size_t per_th = temp_dso.tls_size + temp_dso.tls_align |
| 522 | + sizeof(void *) * (tls_cnt+3); |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 523 | n_th = libc.threads_minus_1 + 1; |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 524 | if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX; |
| 525 | else alloc_size += n_th * per_th; |
| 526 | } |
| 527 | p = calloc(1, alloc_size); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 528 | if (!p) { |
Rich Felker | 74025c8 | 2013-02-02 00:59:25 -0500 | [diff] [blame] | 529 | munmap(map, temp_dso.map_len); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 530 | return 0; |
| 531 | } |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 532 | memcpy(p, &temp_dso, sizeof temp_dso); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 533 | decode_dyn(p); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 534 | p->dev = st.st_dev; |
| 535 | p->ino = st.st_ino; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 536 | p->refcnt = 1; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 537 | p->name = p->buf; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 538 | strcpy(p->name, pathname); |
| 539 | /* Add a shortname only if name arg was not an explicit pathname. */ |
| 540 | if (pathname != name) p->shortname = strrchr(p->name, '/')+1; |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 541 | if (p->tls_image) { |
Rich Felker | 017bf14 | 2012-10-07 20:19:11 -0400 | [diff] [blame] | 542 | if (runtime && !__pthread_self_init()) { |
Rich Felker | 74025c8 | 2013-02-02 00:59:25 -0500 | [diff] [blame] | 543 | munmap(map, p->map_len); |
Rich Felker | 92e1cd9 | 2012-10-06 16:56:35 -0400 | [diff] [blame] | 544 | free(p); |
Rich Felker | 92e1cd9 | 2012-10-06 16:56:35 -0400 | [diff] [blame] | 545 | return 0; |
| 546 | } |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 547 | p->tls_id = ++tls_cnt; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 548 | tls_align = MAXP2(tls_align, p->tls_align); |
Rich Felker | 9ec4283 | 2012-10-15 18:51:53 -0400 | [diff] [blame] | 549 | #ifdef TLS_ABOVE_TP |
| 550 | p->tls_offset = tls_offset + ( (tls_align-1) & |
| 551 | -(tls_offset + (uintptr_t)p->tls_image) ); |
| 552 | tls_offset += p->tls_size; |
| 553 | #else |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 554 | tls_offset += p->tls_size + p->tls_align - 1; |
| 555 | tls_offset -= (tls_offset + (uintptr_t)p->tls_image) |
| 556 | & (p->tls_align-1); |
| 557 | p->tls_offset = tls_offset; |
Rich Felker | 9ec4283 | 2012-10-15 18:51:53 -0400 | [diff] [blame] | 558 | #endif |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 559 | p->new_dtv = (void *)(-sizeof(size_t) & |
| 560 | (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t))); |
| 561 | p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1)); |
| 562 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 563 | |
| 564 | tail->next = p; |
| 565 | p->prev = tail; |
| 566 | tail = p; |
| 567 | |
Rich Felker | 1d7c4f8 | 2012-12-15 23:34:08 -0500 | [diff] [blame] | 568 | if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 569 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 570 | return p; |
| 571 | } |
| 572 | |
| 573 | static void load_deps(struct dso *p) |
| 574 | { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 575 | size_t i, ndeps=0; |
| 576 | struct dso ***deps = &p->deps, **tmp, *dep; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 577 | for (; p; p=p->next) { |
| 578 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 579 | if (p->dynv[i] != DT_RPATH) continue; |
| 580 | r_path = (void *)(p->strings + p->dynv[i+1]); |
| 581 | } |
| 582 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 583 | if (p->dynv[i] != DT_NEEDED) continue; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 584 | dep = load_library(p->strings + p->dynv[i+1]); |
| 585 | if (!dep) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 586 | snprintf(errbuf, sizeof errbuf, |
| 587 | "Error loading shared library %s: %m (needed by %s)", |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 588 | p->strings + p->dynv[i+1], p->name); |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 589 | if (runtime) longjmp(rtld_fail, 1); |
| 590 | dprintf(2, "%s\n", errbuf); |
Rich Felker | 0410950 | 2012-08-18 16:00:23 -0400 | [diff] [blame] | 591 | ldso_fail = 1; |
| 592 | continue; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 593 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 594 | if (runtime) { |
| 595 | tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2)); |
| 596 | if (!tmp) longjmp(rtld_fail, 1); |
| 597 | tmp[ndeps++] = dep; |
| 598 | tmp[ndeps] = 0; |
| 599 | *deps = tmp; |
| 600 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 601 | } |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 602 | r_path = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 606 | static void load_preload(char *s) |
| 607 | { |
| 608 | int tmp; |
| 609 | char *z; |
| 610 | for (z=s; *z; s=z) { |
| 611 | for ( ; *s && isspace(*s); s++); |
| 612 | for (z=s; *z && !isspace(*z); z++); |
| 613 | tmp = *z; |
| 614 | *z = 0; |
| 615 | load_library(s); |
| 616 | *z = tmp; |
| 617 | } |
| 618 | } |
| 619 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 620 | static void make_global(struct dso *p) |
| 621 | { |
| 622 | for (; p; p=p->next) p->global = 1; |
| 623 | } |
| 624 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 625 | static void reloc_all(struct dso *p) |
| 626 | { |
| 627 | size_t dyn[DYN_CNT] = {0}; |
| 628 | for (; p; p=p->next) { |
| 629 | if (p->relocated) continue; |
| 630 | decode_vec(p->dynv, dyn, DYN_CNT); |
Rich Felker | babf820 | 2012-08-05 12:50:26 -0400 | [diff] [blame] | 631 | #ifdef NEED_ARCH_RELOCS |
| 632 | do_arch_relocs(p, head); |
| 633 | #endif |
Rich Felker | 87d13a4 | 2012-08-05 02:49:02 -0400 | [diff] [blame] | 634 | do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], |
| 635 | 2+(dyn[DT_PLTREL]==DT_RELA)); |
| 636 | do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2); |
| 637 | do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3); |
Rich Felker | 368ba4a | 2011-06-25 00:18:19 -0400 | [diff] [blame] | 638 | p->relocated = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 642 | static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride) |
| 643 | { |
| 644 | for (; cnt--; ph = (void *)((char *)ph + stride)) |
| 645 | if (ph->p_type == PT_DYNAMIC) |
| 646 | return ph->p_vaddr; |
| 647 | return 0; |
| 648 | } |
| 649 | |
Rich Felker | f419bcb | 2012-08-26 21:09:26 -0400 | [diff] [blame] | 650 | static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p) |
| 651 | { |
| 652 | size_t min_addr = -1, max_addr = 0; |
| 653 | for (; cnt--; ph = (void *)((char *)ph + stride)) { |
| 654 | if (ph->p_type != PT_LOAD) continue; |
| 655 | if (ph->p_vaddr < min_addr) |
| 656 | min_addr = ph->p_vaddr; |
| 657 | if (ph->p_vaddr+ph->p_memsz > max_addr) |
| 658 | max_addr = ph->p_vaddr+ph->p_memsz; |
| 659 | } |
| 660 | min_addr &= -PAGE_SIZE; |
| 661 | max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE; |
| 662 | p->map = p->base + min_addr; |
| 663 | p->map_len = max_addr - min_addr; |
| 664 | } |
| 665 | |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 666 | static void do_fini() |
| 667 | { |
| 668 | struct dso *p; |
| 669 | size_t dyn[DYN_CNT] = {0}; |
| 670 | for (p=fini_head; p; p=p->fini_next) { |
| 671 | if (!p->constructed) continue; |
| 672 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 673 | ((void (*)(void))(p->base + dyn[DT_FINI]))(); |
| 674 | } |
| 675 | } |
| 676 | |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 677 | static void do_init_fini(struct dso *p) |
| 678 | { |
| 679 | size_t dyn[DYN_CNT] = {0}; |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 680 | int need_locking = libc.threads_minus_1; |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 681 | /* Allow recursive calls that arise when a library calls |
| 682 | * dlopen from one of its constructors, but block any |
| 683 | * other threads until all ctors have finished. */ |
| 684 | if (need_locking) pthread_mutex_lock(&init_fini_lock); |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 685 | for (; p; p=p->prev) { |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 686 | if (p->constructed) continue; |
| 687 | p->constructed = 1; |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 688 | decode_vec(p->dynv, dyn, DYN_CNT); |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 689 | if (dyn[0] & (1<<DT_FINI)) { |
| 690 | p->fini_next = fini_head; |
| 691 | fini_head = p; |
| 692 | } |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 693 | if (dyn[0] & (1<<DT_INIT)) |
| 694 | ((void (*)(void))(p->base + dyn[DT_INIT]))(); |
Rich Felker | 509b50e | 2013-06-29 02:24:02 -0400 | [diff] [blame^] | 695 | if (!need_locking && libc.threads_minus_1) { |
| 696 | need_locking = 1; |
| 697 | pthread_mutex_lock(&init_fini_lock); |
| 698 | } |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 699 | } |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 700 | if (need_locking) pthread_mutex_unlock(&init_fini_lock); |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 701 | } |
| 702 | |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 703 | void _dl_debug_state(void) |
| 704 | { |
| 705 | } |
| 706 | |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 707 | void *__copy_tls(unsigned char *mem) |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 708 | { |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 709 | pthread_t td; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 710 | struct dso *p; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 711 | |
| 712 | if (!tls_cnt) return mem; |
| 713 | |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 714 | void **dtv = (void *)mem; |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 715 | dtv[0] = (void *)tls_cnt; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 716 | |
Rich Felker | 9ec4283 | 2012-10-15 18:51:53 -0400 | [diff] [blame] | 717 | #ifdef TLS_ABOVE_TP |
| 718 | mem += sizeof(void *) * (tls_cnt+1); |
| 719 | mem += -((uintptr_t)mem + sizeof(struct pthread)) & (tls_align-1); |
| 720 | td = (pthread_t)mem; |
| 721 | mem += sizeof(struct pthread); |
| 722 | |
| 723 | for (p=head; p; p=p->next) { |
| 724 | if (!p->tls_id) continue; |
| 725 | dtv[p->tls_id] = mem + p->tls_offset; |
| 726 | memcpy(dtv[p->tls_id], p->tls_image, p->tls_len); |
| 727 | } |
| 728 | #else |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 729 | mem += libc.tls_size - sizeof(struct pthread); |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 730 | mem -= (uintptr_t)mem & (tls_align-1); |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 731 | td = (pthread_t)mem; |
| 732 | |
| 733 | for (p=head; p; p=p->next) { |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 734 | if (!p->tls_id) continue; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 735 | dtv[p->tls_id] = mem - p->tls_offset; |
| 736 | memcpy(dtv[p->tls_id], p->tls_image, p->tls_len); |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 737 | } |
Rich Felker | 9ec4283 | 2012-10-15 18:51:53 -0400 | [diff] [blame] | 738 | #endif |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 739 | td->dtv = dtv; |
| 740 | return td; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 741 | } |
| 742 | |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 743 | void *__tls_get_addr(size_t *v) |
Rich Felker | 9b153c0 | 2012-10-04 21:01:56 -0400 | [diff] [blame] | 744 | { |
| 745 | pthread_t self = __pthread_self(); |
Rich Felker | 44b4d09 | 2013-06-03 16:35:59 -0400 | [diff] [blame] | 746 | if (v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]]) |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 747 | return (char *)self->dtv[v[0]]+v[1]; |
| 748 | |
| 749 | /* Block signals to make accessing new TLS async-signal-safe */ |
| 750 | sigset_t set; |
Rich Felker | 00902c7 | 2012-10-06 23:57:51 -0400 | [diff] [blame] | 751 | pthread_sigmask(SIG_BLOCK, SIGALL_SET, &set); |
Rich Felker | 44b4d09 | 2013-06-03 16:35:59 -0400 | [diff] [blame] | 752 | if (v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]]) { |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 753 | pthread_sigmask(SIG_SETMASK, &set, 0); |
| 754 | return (char *)self->dtv[v[0]]+v[1]; |
Rich Felker | 9b153c0 | 2012-10-04 21:01:56 -0400 | [diff] [blame] | 755 | } |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 756 | |
| 757 | /* This is safe without any locks held because, if the caller |
| 758 | * is able to request the Nth entry of the DTV, the DSO list |
| 759 | * must be valid at least that far out and it was synchronized |
| 760 | * at program startup or by an already-completed call to dlopen. */ |
| 761 | struct dso *p; |
| 762 | for (p=head; p->tls_id != v[0]; p=p->next); |
| 763 | |
| 764 | /* Get new DTV space from new DSO if needed */ |
Rich Felker | 44b4d09 | 2013-06-03 16:35:59 -0400 | [diff] [blame] | 765 | if (v[0] > (size_t)self->dtv[0]) { |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 766 | void **newdtv = p->new_dtv + |
| 767 | (v[0]+1)*sizeof(void *)*a_fetch_add(&p->new_dtv_idx,1); |
Rich Felker | 44b4d09 | 2013-06-03 16:35:59 -0400 | [diff] [blame] | 768 | memcpy(newdtv, self->dtv, |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 769 | ((size_t)self->dtv[0]+1) * sizeof(void *)); |
| 770 | newdtv[0] = (void *)v[0]; |
| 771 | self->dtv = newdtv; |
| 772 | } |
| 773 | |
| 774 | /* Get new TLS memory from new DSO */ |
| 775 | unsigned char *mem = p->new_tls + |
| 776 | (p->tls_size + p->tls_align) * a_fetch_add(&p->new_tls_idx,1); |
| 777 | mem += ((uintptr_t)p->tls_image - (uintptr_t)mem) & (p->tls_align-1); |
| 778 | self->dtv[v[0]] = mem; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 779 | memcpy(mem, p->tls_image, p->tls_len); |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 780 | pthread_sigmask(SIG_SETMASK, &set, 0); |
| 781 | return mem + v[1]; |
Rich Felker | 9b153c0 | 2012-10-04 21:01:56 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 784 | static void update_tls_size() |
| 785 | { |
Rich Felker | 9ec4283 | 2012-10-15 18:51:53 -0400 | [diff] [blame] | 786 | libc.tls_size = ALIGN( |
| 787 | (1+tls_cnt) * sizeof(void *) + |
| 788 | tls_offset + |
| 789 | sizeof(struct pthread) + |
| 790 | tls_align * 2, |
| 791 | tls_align); |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 792 | } |
| 793 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 794 | void *__dynlink(int argc, char **argv) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 795 | { |
Rich Felker | 731e8ff | 2012-08-25 17:24:46 -0400 | [diff] [blame] | 796 | size_t aux[AUX_CNT] = {0}; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 797 | size_t i; |
| 798 | Phdr *phdr; |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 799 | Ehdr *ehdr; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 800 | static struct dso builtin_dsos[3]; |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 801 | struct dso *const app = builtin_dsos+0; |
| 802 | struct dso *const lib = builtin_dsos+1; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 803 | struct dso *const vdso = builtin_dsos+2; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 804 | char *env_preload=0; |
Rich Felker | dbcb3ad | 2012-08-25 17:31:59 -0400 | [diff] [blame] | 805 | size_t vdso_base; |
Rich Felker | 2f2f115 | 2012-11-01 23:49:57 -0400 | [diff] [blame] | 806 | size_t *auxv; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 807 | |
| 808 | /* Find aux vector just past environ[] */ |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 809 | for (i=argc+1; argv[i]; i++) |
| 810 | if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16)) |
| 811 | env_path = argv[i]+16; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 812 | else if (!memcmp(argv[i], "LD_PRELOAD=", 11)) |
| 813 | env_preload = argv[i]+11; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 814 | auxv = (void *)(argv+i+1); |
| 815 | |
| 816 | decode_vec(auxv, aux, AUX_CNT); |
| 817 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 818 | /* Only trust user/env if kernel says we're not suid/sgid */ |
| 819 | if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] |
Rich Felker | a045883 | 2011-08-16 07:46:42 -0400 | [diff] [blame] | 820 | || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) { |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 821 | env_path = 0; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 822 | env_preload = 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 823 | } |
| 824 | |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 825 | /* If the dynamic linker was invoked as a program itself, AT_BASE |
| 826 | * will not be set. In that case, we assume the base address is |
| 827 | * the start of the page containing the PHDRs; I don't know any |
| 828 | * better approach... */ |
| 829 | if (!aux[AT_BASE]) { |
| 830 | aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE; |
| 831 | aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0; |
| 832 | } |
| 833 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 834 | /* The dynamic linker load address is passed by the kernel |
| 835 | * in the AUX vector, so this is easy. */ |
| 836 | lib->base = (void *)aux[AT_BASE]; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 837 | lib->name = lib->shortname = "libc.so"; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 838 | lib->global = 1; |
| 839 | ehdr = (void *)lib->base; |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 840 | lib->phnum = ehdr->e_phnum; |
| 841 | lib->phdr = (void *)(aux[AT_BASE]+ehdr->e_phoff); |
| 842 | find_map_range(lib->phdr, ehdr->e_phnum, ehdr->e_phentsize, lib); |
| 843 | lib->dynv = (void *)(lib->base + find_dyn(lib->phdr, |
| 844 | ehdr->e_phnum, ehdr->e_phentsize)); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 845 | decode_dyn(lib); |
| 846 | |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 847 | if (aux[AT_PHDR]) { |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame] | 848 | size_t interp_off = 0; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 849 | size_t tls_image = 0; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 850 | /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */ |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 851 | app->phdr = phdr = (void *)aux[AT_PHDR]; |
| 852 | app->phnum = aux[AT_PHNUM]; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 853 | for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) { |
| 854 | if (phdr->p_type == PT_PHDR) |
| 855 | app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr); |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame] | 856 | else if (phdr->p_type == PT_INTERP) |
| 857 | interp_off = (size_t)phdr->p_vaddr; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 858 | else if (phdr->p_type == PT_TLS) { |
| 859 | tls_image = phdr->p_vaddr; |
| 860 | app->tls_len = phdr->p_filesz; |
| 861 | app->tls_size = phdr->p_memsz; |
| 862 | app->tls_align = phdr->p_align; |
| 863 | } |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 864 | } |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 865 | if (app->tls_size) app->tls_image = (char *)app->base + tls_image; |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame] | 866 | if (interp_off) lib->name = (char *)app->base + interp_off; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 867 | app->name = argv[0]; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 868 | app->dynv = (void *)(app->base + find_dyn( |
| 869 | (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT])); |
Rich Felker | f419bcb | 2012-08-26 21:09:26 -0400 | [diff] [blame] | 870 | find_map_range((void *)aux[AT_PHDR], |
| 871 | aux[AT_PHNUM], aux[AT_PHENT], app); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 872 | } else { |
| 873 | int fd; |
| 874 | char *ldname = argv[0]; |
Rich Felker | a617a8e | 2012-11-01 23:46:39 -0400 | [diff] [blame] | 875 | size_t l = strlen(ldname); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 876 | if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1; |
| 877 | *argv++ = (void *)-1; |
| 878 | if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1; |
| 879 | if (!argv[0]) { |
| 880 | dprintf(2, "musl libc/dynamic program loader\n"); |
| 881 | dprintf(2, "usage: %s pathname%s\n", ldname, |
| 882 | ldd_mode ? "" : " [args]"); |
| 883 | _exit(1); |
| 884 | } |
| 885 | fd = open(argv[0], O_RDONLY); |
| 886 | if (fd < 0) { |
| 887 | dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno)); |
| 888 | _exit(1); |
| 889 | } |
| 890 | runtime = 1; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 891 | ehdr = (void *)map_library(fd, app); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 892 | if (!ehdr) { |
| 893 | dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]); |
| 894 | _exit(1); |
| 895 | } |
| 896 | runtime = 0; |
| 897 | close(fd); |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame] | 898 | lib->name = ldname; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 899 | app->name = argv[0]; |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 900 | app->phnum = ehdr->e_phnum; |
| 901 | app->phdr = (void *)(app->base + ehdr->e_phoff); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 902 | aux[AT_ENTRY] = ehdr->e_entry; |
Rich Felker | e12fe65 | 2012-01-23 02:02:59 -0500 | [diff] [blame] | 903 | } |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 904 | if (app->tls_size) { |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 905 | app->tls_id = tls_cnt = 1; |
Rich Felker | 9ec4283 | 2012-10-15 18:51:53 -0400 | [diff] [blame] | 906 | #ifdef TLS_ABOVE_TP |
| 907 | app->tls_offset = 0; |
| 908 | tls_offset = app->tls_size |
| 909 | + ( -((uintptr_t)app->tls_image + app->tls_size) |
| 910 | & (app->tls_align-1) ); |
| 911 | #else |
Rich Felker | c62b9f3 | 2012-10-14 19:56:50 -0400 | [diff] [blame] | 912 | tls_offset = app->tls_offset = app->tls_size |
| 913 | + ( -((uintptr_t)app->tls_image + app->tls_size) |
| 914 | & (app->tls_align-1) ); |
Rich Felker | 9ec4283 | 2012-10-15 18:51:53 -0400 | [diff] [blame] | 915 | #endif |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 916 | tls_align = MAXP2(tls_align, app->tls_align); |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 917 | } |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 918 | app->global = 1; |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 919 | app->constructed = 1; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 920 | decode_dyn(app); |
| 921 | |
| 922 | /* Attach to vdso, if provided by the kernel */ |
Rich Felker | dbcb3ad | 2012-08-25 17:31:59 -0400 | [diff] [blame] | 923 | if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) { |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 924 | ehdr = (void *)vdso_base; |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 925 | vdso->phdr = phdr = (void *)(vdso_base + ehdr->e_phoff); |
| 926 | vdso->phnum = ehdr->e_phnum; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 927 | for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) { |
| 928 | if (phdr->p_type == PT_DYNAMIC) |
| 929 | vdso->dynv = (void *)(vdso_base + phdr->p_offset); |
| 930 | if (phdr->p_type == PT_LOAD) |
| 931 | vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset); |
| 932 | } |
Rich Felker | 75a31fa | 2012-11-25 20:56:31 -0500 | [diff] [blame] | 933 | vdso->name = ""; |
| 934 | vdso->shortname = "linux-gate.so.1"; |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 935 | vdso->global = 1; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 936 | decode_dyn(vdso); |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 937 | vdso->prev = lib; |
| 938 | lib->next = vdso; |
| 939 | } |
| 940 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 941 | /* Initial dso chain consists only of the app. We temporarily |
| 942 | * append the dynamic linker/libc so we can relocate it, then |
| 943 | * restore the initial chain in preparation for loading third |
| 944 | * party libraries (preload/needed). */ |
| 945 | head = tail = app; |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 946 | ldso = lib; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 947 | app->next = lib; |
| 948 | reloc_all(lib); |
| 949 | app->next = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 950 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 951 | /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */ |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 952 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 953 | /* Donate unused parts of app and library mapping to malloc */ |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 954 | reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]); |
| 955 | ehdr = (void *)lib->base; |
| 956 | reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff), |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 957 | ehdr->e_phentsize, ehdr->e_phnum); |
| 958 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 959 | /* Load preload/needed libraries, add their symbols to the global |
Rich Felker | fd7015d | 2012-01-23 18:32:40 -0500 | [diff] [blame] | 960 | * namespace, and perform all remaining relocations. The main |
| 961 | * program must be relocated LAST since it may contain copy |
| 962 | * relocations which depend on libraries' relocations. */ |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 963 | if (env_preload) load_preload(env_preload); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 964 | load_deps(app); |
| 965 | make_global(app); |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 966 | |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 967 | reloc_all(app->next); |
| 968 | reloc_all(app); |
| 969 | |
| 970 | update_tls_size(); |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 971 | if (tls_cnt) { |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 972 | void *mem = mmap(0, libc.tls_size, PROT_READ|PROT_WRITE, |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 973 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 974 | if (mem==MAP_FAILED || |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 975 | !__install_initial_tls(__copy_tls(mem))) { |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 976 | dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n", |
Rich Felker | e23d358 | 2012-10-13 23:25:20 -0400 | [diff] [blame] | 977 | argv[0], libc.tls_size); |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 978 | _exit(127); |
| 979 | } |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 980 | } |
| 981 | |
Rich Felker | 0410950 | 2012-08-18 16:00:23 -0400 | [diff] [blame] | 982 | if (ldso_fail) _exit(127); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 983 | if (ldd_mode) _exit(0); |
| 984 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 985 | /* Switch to runtime mode: any further failures in the dynamic |
| 986 | * linker are a reportable failure rather than a fatal startup |
| 987 | * error. If the dynamic loader (dlopen) will not be used, free |
| 988 | * all memory used by the dynamic linker. */ |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 989 | runtime = 1; |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 990 | |
Rich Felker | 7d9a5c6 | 2012-08-05 14:03:17 -0400 | [diff] [blame] | 991 | #ifndef DYNAMIC_IS_RO |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 992 | for (i=0; app->dynv[i]; i+=2) |
| 993 | if (app->dynv[i]==DT_DEBUG) |
| 994 | app->dynv[i+1] = (size_t)&debug; |
Rich Felker | 7d9a5c6 | 2012-08-05 14:03:17 -0400 | [diff] [blame] | 995 | #endif |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 996 | debug.ver = 1; |
| 997 | debug.bp = _dl_debug_state; |
| 998 | debug.head = head; |
| 999 | debug.base = lib->base; |
| 1000 | debug.state = 0; |
| 1001 | _dl_debug_state(); |
| 1002 | |
Rich Felker | 0a96a37 | 2012-10-07 21:43:46 -0400 | [diff] [blame] | 1003 | if (ssp_used) __init_ssp((void *)aux[AT_RANDOM]); |
Rich Felker | 58aa5f4 | 2012-05-03 20:42:45 -0400 | [diff] [blame] | 1004 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 1005 | errno = 0; |
| 1006 | return (void *)aux[AT_ENTRY]; |
| 1007 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1008 | |
Rich Felker | a7936f6 | 2012-11-30 17:56:23 -0500 | [diff] [blame] | 1009 | void __init_ldso_ctors(void) |
| 1010 | { |
| 1011 | atexit(do_fini); |
| 1012 | do_init_fini(tail); |
| 1013 | } |
| 1014 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1015 | void *dlopen(const char *file, int mode) |
| 1016 | { |
Rich Felker | 642b759 | 2012-10-05 01:15:25 -0400 | [diff] [blame] | 1017 | struct dso *volatile p, *orig_tail, *next; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 1018 | size_t orig_tls_cnt, orig_tls_offset, orig_tls_align; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1019 | size_t i; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 1020 | int cs; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1021 | |
| 1022 | if (!file) return head; |
| 1023 | |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 1024 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1025 | pthread_rwlock_wrlock(&lock); |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 1026 | __inhibit_ptc(); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1027 | |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 1028 | p = 0; |
| 1029 | orig_tls_cnt = tls_cnt; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 1030 | orig_tls_offset = tls_offset; |
| 1031 | orig_tls_align = tls_align; |
Rich Felker | 642b759 | 2012-10-05 01:15:25 -0400 | [diff] [blame] | 1032 | orig_tail = tail; |
Rich Felker | 4d07e55 | 2013-01-23 22:07:45 -0500 | [diff] [blame] | 1033 | noload = mode & RTLD_NOLOAD; |
Rich Felker | 642b759 | 2012-10-05 01:15:25 -0400 | [diff] [blame] | 1034 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1035 | if (setjmp(rtld_fail)) { |
| 1036 | /* Clean up anything new that was (partially) loaded */ |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 1037 | if (p && p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 1038 | if (p->deps[i]->global < 0) |
| 1039 | p->deps[i]->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1040 | for (p=orig_tail->next; p; p=next) { |
| 1041 | next = p->next; |
| 1042 | munmap(p->map, p->map_len); |
| 1043 | free(p->deps); |
| 1044 | free(p); |
| 1045 | } |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 1046 | tls_cnt = orig_tls_cnt; |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 1047 | tls_offset = orig_tls_offset; |
| 1048 | tls_align = orig_tls_align; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1049 | tail = orig_tail; |
| 1050 | tail->next = 0; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 1051 | p = 0; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 1052 | errflag = 1; |
| 1053 | goto end; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 1054 | } else p = load_library(file); |
| 1055 | |
| 1056 | if (!p) { |
Rich Felker | 4d07e55 | 2013-01-23 22:07:45 -0500 | [diff] [blame] | 1057 | snprintf(errbuf, sizeof errbuf, noload ? |
| 1058 | "Library %s is not already loaded" : |
| 1059 | "Error loading shared library %s: %m", |
| 1060 | file); |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 1061 | errflag = 1; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 1062 | goto end; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1063 | } |
| 1064 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1065 | /* First load handling */ |
| 1066 | if (!p->deps) { |
| 1067 | load_deps(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 1068 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 1069 | if (!p->deps[i]->global) |
| 1070 | p->deps[i]->global = -1; |
| 1071 | if (!p->global) p->global = -1; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1072 | reloc_all(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 1073 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 1074 | if (p->deps[i]->global < 0) |
| 1075 | p->deps[i]->global = 0; |
| 1076 | if (p->global < 0) p->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | if (mode & RTLD_GLOBAL) { |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 1080 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1081 | p->deps[i]->global = 1; |
| 1082 | p->global = 1; |
| 1083 | } |
| 1084 | |
Rich Felker | cf3fd3d | 2012-10-06 01:22:51 -0400 | [diff] [blame] | 1085 | update_tls_size(); |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 1086 | |
Rich Felker | 2f2f115 | 2012-11-01 23:49:57 -0400 | [diff] [blame] | 1087 | if (ssp_used) __init_ssp(libc.auxv); |
Rich Felker | 731e8ff | 2012-08-25 17:24:46 -0400 | [diff] [blame] | 1088 | |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 1089 | _dl_debug_state(); |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 1090 | orig_tail = tail; |
Rich Felker | 06933cc | 2011-06-26 22:09:32 -0400 | [diff] [blame] | 1091 | end: |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame] | 1092 | __release_ptc(); |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 1093 | if (p) gencnt++; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1094 | pthread_rwlock_unlock(&lock); |
Rich Felker | f4f77c0 | 2012-10-05 13:09:09 -0400 | [diff] [blame] | 1095 | if (p) do_init_fini(orig_tail); |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 1096 | pthread_setcancelstate(cs, 0); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1097 | return p; |
| 1098 | } |
| 1099 | |
Rich Felker | 4d98280 | 2013-01-16 11:49:00 -0500 | [diff] [blame] | 1100 | static int invalid_dso_handle(void *h) |
Rich Felker | 6468fc9 | 2013-01-10 14:05:40 -0500 | [diff] [blame] | 1101 | { |
| 1102 | struct dso *p; |
| 1103 | for (p=head; p; p=p->next) if (h==p) return 0; |
| 1104 | snprintf(errbuf, sizeof errbuf, "Invalid library handle %p", (void *)h); |
| 1105 | errflag = 1; |
| 1106 | return 1; |
| 1107 | } |
| 1108 | |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 1109 | static void *do_dlsym(struct dso *p, const char *s, void *ra) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1110 | { |
| 1111 | size_t i; |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 1112 | uint32_t h = 0, gh = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1113 | Sym *sym; |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 1114 | if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) { |
Rich Felker | deb15b3 | 2012-10-19 21:41:30 -0400 | [diff] [blame] | 1115 | if (p == RTLD_DEFAULT) { |
| 1116 | p = head; |
| 1117 | } else if (p == RTLD_NEXT) { |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 1118 | for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next); |
| 1119 | if (!p) p=head; |
Rich Felker | deb15b3 | 2012-10-19 21:41:30 -0400 | [diff] [blame] | 1120 | p = p->next; |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 1121 | } |
Rich Felker | deb15b3 | 2012-10-19 21:41:30 -0400 | [diff] [blame] | 1122 | struct symdef def = find_sym(p, s, 0); |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 1123 | if (!def.sym) goto failed; |
Rich Felker | 0a1c2c1 | 2012-10-19 21:57:56 -0400 | [diff] [blame] | 1124 | if ((def.sym->st_info&0xf) == STT_TLS) |
| 1125 | return __tls_get_addr((size_t []){def.dso->tls_id, def.sym->st_value}); |
Rich Felker | 9c74856 | 2012-10-04 22:48:33 -0400 | [diff] [blame] | 1126 | return def.dso->base + def.sym->st_value; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 1127 | } |
Rich Felker | 637dd2d | 2013-01-23 20:21:36 -0500 | [diff] [blame] | 1128 | if (p != RTLD_DEFAULT && p != RTLD_NEXT && invalid_dso_handle(p)) |
| 1129 | return 0; |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 1130 | if (p->ghashtab) { |
| 1131 | gh = gnu_hash(s); |
| 1132 | sym = gnu_lookup(s, gh, p); |
| 1133 | } else { |
| 1134 | h = sysv_hash(s); |
| 1135 | sym = sysv_lookup(s, h, p); |
| 1136 | } |
Rich Felker | 0a1c2c1 | 2012-10-19 21:57:56 -0400 | [diff] [blame] | 1137 | if (sym && (sym->st_info&0xf) == STT_TLS) |
| 1138 | return __tls_get_addr((size_t []){p->tls_id, sym->st_value}); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1139 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 1140 | return p->base + sym->st_value; |
| 1141 | if (p->deps) for (i=0; p->deps[i]; i++) { |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 1142 | if (p->deps[i]->ghashtab) { |
| 1143 | if (!gh) gh = gnu_hash(s); |
Rich Felker | a5d6199 | 2012-08-25 17:40:27 -0400 | [diff] [blame] | 1144 | sym = gnu_lookup(s, gh, p->deps[i]); |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 1145 | } else { |
| 1146 | if (!h) h = sysv_hash(s); |
| 1147 | sym = sysv_lookup(s, h, p->deps[i]); |
| 1148 | } |
Rich Felker | 0a1c2c1 | 2012-10-19 21:57:56 -0400 | [diff] [blame] | 1149 | if (sym && (sym->st_info&0xf) == STT_TLS) |
| 1150 | return __tls_get_addr((size_t []){p->deps[i]->tls_id, sym->st_value}); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1151 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 1152 | return p->deps[i]->base + sym->st_value; |
| 1153 | } |
Rich Felker | 4027f4e | 2012-05-04 20:18:18 -0400 | [diff] [blame] | 1154 | failed: |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 1155 | errflag = 1; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 1156 | snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1157 | return 0; |
| 1158 | } |
| 1159 | |
Rich Felker | f419bcb | 2012-08-26 21:09:26 -0400 | [diff] [blame] | 1160 | int __dladdr(void *addr, Dl_info *info) |
| 1161 | { |
| 1162 | struct dso *p; |
| 1163 | Sym *sym; |
| 1164 | uint32_t nsym; |
| 1165 | char *strings; |
| 1166 | size_t i; |
| 1167 | void *best = 0; |
| 1168 | char *bestname; |
| 1169 | |
| 1170 | pthread_rwlock_rdlock(&lock); |
| 1171 | for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next); |
| 1172 | pthread_rwlock_unlock(&lock); |
| 1173 | |
| 1174 | if (!p) return 0; |
| 1175 | |
| 1176 | sym = p->syms; |
| 1177 | strings = p->strings; |
| 1178 | if (p->hashtab) { |
| 1179 | nsym = p->hashtab[1]; |
| 1180 | } else { |
| 1181 | uint32_t *buckets; |
| 1182 | uint32_t *hashval; |
| 1183 | buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4); |
| 1184 | sym += p->ghashtab[1]; |
| 1185 | for (i = 0; i < p->ghashtab[0]; i++) { |
| 1186 | if (buckets[i] > nsym) |
| 1187 | nsym = buckets[i]; |
| 1188 | } |
| 1189 | if (nsym) { |
| 1190 | nsym -= p->ghashtab[1]; |
| 1191 | hashval = buckets + p->ghashtab[0] + nsym; |
| 1192 | do nsym++; |
| 1193 | while (!(*hashval++ & 1)); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | for (; nsym; nsym--, sym++) { |
Rich Felker | cdc5c74 | 2013-01-16 11:47:35 -0500 | [diff] [blame] | 1198 | if (sym->st_value |
Rich Felker | f419bcb | 2012-08-26 21:09:26 -0400 | [diff] [blame] | 1199 | && (1<<(sym->st_info&0xf) & OK_TYPES) |
| 1200 | && (1<<(sym->st_info>>4) & OK_BINDS)) { |
| 1201 | void *symaddr = p->base + sym->st_value; |
| 1202 | if (symaddr > addr || symaddr < best) |
| 1203 | continue; |
| 1204 | best = symaddr; |
| 1205 | bestname = strings + sym->st_name; |
| 1206 | if (addr == symaddr) |
| 1207 | break; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | if (!best) return 0; |
| 1212 | |
| 1213 | info->dli_fname = p->name; |
| 1214 | info->dli_fbase = p->base; |
| 1215 | info->dli_sname = bestname; |
| 1216 | info->dli_saddr = best; |
| 1217 | |
| 1218 | return 1; |
| 1219 | } |
| 1220 | |
Rich Felker | 400c5e5 | 2012-09-06 22:44:55 -0400 | [diff] [blame] | 1221 | void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1222 | { |
| 1223 | void *res; |
| 1224 | pthread_rwlock_rdlock(&lock); |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 1225 | res = do_dlsym(p, s, ra); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1226 | pthread_rwlock_unlock(&lock); |
| 1227 | return res; |
| 1228 | } |
Rich Felker | 18c0e02 | 2012-10-31 21:27:48 -0400 | [diff] [blame] | 1229 | |
| 1230 | int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data) |
| 1231 | { |
| 1232 | struct dso *current; |
| 1233 | struct dl_phdr_info info; |
| 1234 | int ret = 0; |
| 1235 | for(current = head; current;) { |
| 1236 | info.dlpi_addr = (uintptr_t)current->base; |
| 1237 | info.dlpi_name = current->name; |
| 1238 | info.dlpi_phdr = current->phdr; |
| 1239 | info.dlpi_phnum = current->phnum; |
| 1240 | info.dlpi_adds = gencnt; |
| 1241 | info.dlpi_subs = 0; |
| 1242 | info.dlpi_tls_modid = current->tls_id; |
| 1243 | info.dlpi_tls_data = current->tls_image; |
| 1244 | |
| 1245 | ret = (callback)(&info, sizeof (info), data); |
| 1246 | |
| 1247 | if (ret != 0) break; |
| 1248 | |
| 1249 | pthread_rwlock_rdlock(&lock); |
| 1250 | current = current->next; |
| 1251 | pthread_rwlock_unlock(&lock); |
| 1252 | } |
| 1253 | return ret; |
| 1254 | } |
Rich Felker | 5a09a53 | 2012-02-03 03:16:07 -0500 | [diff] [blame] | 1255 | #else |
Rich Felker | 4d98280 | 2013-01-16 11:49:00 -0500 | [diff] [blame] | 1256 | static int invalid_dso_handle(void *h) |
Rich Felker | 6468fc9 | 2013-01-10 14:05:40 -0500 | [diff] [blame] | 1257 | { |
| 1258 | snprintf(errbuf, sizeof errbuf, "Invalid library handle %p", (void *)h); |
| 1259 | errflag = 1; |
| 1260 | return 1; |
| 1261 | } |
Rich Felker | 5a09a53 | 2012-02-03 03:16:07 -0500 | [diff] [blame] | 1262 | void *dlopen(const char *file, int mode) |
| 1263 | { |
| 1264 | return 0; |
| 1265 | } |
Rich Felker | 400c5e5 | 2012-09-06 22:44:55 -0400 | [diff] [blame] | 1266 | void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra) |
Rich Felker | 5a09a53 | 2012-02-03 03:16:07 -0500 | [diff] [blame] | 1267 | { |
| 1268 | return 0; |
| 1269 | } |
Rich Felker | f419bcb | 2012-08-26 21:09:26 -0400 | [diff] [blame] | 1270 | int __dladdr (void *addr, Dl_info *info) |
| 1271 | { |
| 1272 | return 0; |
| 1273 | } |
Rich Felker | 5a09a53 | 2012-02-03 03:16:07 -0500 | [diff] [blame] | 1274 | #endif |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1275 | |
| 1276 | char *dlerror() |
| 1277 | { |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 1278 | if (!errflag) return 0; |
| 1279 | errflag = 0; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 1280 | return errbuf; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | int dlclose(void *p) |
| 1284 | { |
Rich Felker | 6468fc9 | 2013-01-10 14:05:40 -0500 | [diff] [blame] | 1285 | return invalid_dso_handle(p); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 1286 | } |