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