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