Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <stdint.h> |
| 6 | #include <elf.h> |
| 7 | #include <sys/mman.h> |
| 8 | #include <limits.h> |
| 9 | #include <stdint.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <errno.h> |
| 13 | #include <limits.h> |
| 14 | #include <elf.h> |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 15 | #include <setjmp.h> |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 16 | #include <pthread.h> |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 17 | #include <ctype.h> |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 18 | #include <dlfcn.h> |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 19 | |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 20 | static int errflag; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 21 | static char errbuf[128]; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 22 | |
| 23 | #ifdef __PIC__ |
| 24 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 25 | #include "reloc.h" |
| 26 | |
| 27 | #if ULONG_MAX == 0xffffffff |
| 28 | typedef Elf32_Ehdr Ehdr; |
| 29 | typedef Elf32_Phdr Phdr; |
| 30 | typedef Elf32_Sym Sym; |
| 31 | #define R_TYPE(x) ((x)&255) |
| 32 | #define R_SYM(x) ((x)>>8) |
| 33 | #else |
| 34 | typedef Elf64_Ehdr Ehdr; |
| 35 | typedef Elf64_Phdr Phdr; |
| 36 | typedef Elf64_Sym Sym; |
| 37 | #define R_TYPE(x) ((x)&0xffffffff) |
| 38 | #define R_SYM(x) ((x)>>32) |
| 39 | #endif |
| 40 | |
| 41 | struct dso |
| 42 | { |
| 43 | struct dso *next, *prev; |
| 44 | int refcnt; |
| 45 | size_t *dynv; |
| 46 | Sym *syms; |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 47 | uint32_t *hashtab; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 48 | char *strings; |
| 49 | unsigned char *base; |
| 50 | unsigned char *map; |
| 51 | size_t map_len; |
| 52 | dev_t dev; |
| 53 | ino_t ino; |
Rich Felker | 700a815 | 2012-02-07 20:29:29 -0500 | [diff] [blame] | 54 | char global; |
| 55 | char relocated; |
| 56 | char constructed; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 57 | struct dso **deps; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 58 | char *name; |
| 59 | char buf[]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 60 | }; |
| 61 | |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame^] | 62 | struct __pthread; |
| 63 | struct __pthread *__pthread_self_init(void); |
| 64 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 65 | static struct dso *head, *tail, *libc; |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 66 | static char *env_path, *sys_path, *r_path; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 67 | static int rtld_used; |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame^] | 68 | static int ssp_used; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 69 | static int runtime; |
| 70 | static jmp_buf rtld_fail; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 71 | static pthread_rwlock_t lock; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 72 | |
Rich Felker | a045883 | 2011-08-16 07:46:42 -0400 | [diff] [blame] | 73 | #define AUX_CNT 24 |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 74 | #define DYN_CNT 34 |
| 75 | |
| 76 | static void decode_vec(size_t *v, size_t *a, size_t cnt) |
| 77 | { |
| 78 | memset(a, 0, cnt*sizeof(size_t)); |
| 79 | for (; v[0]; v+=2) if (v[0]<cnt) { |
| 80 | a[0] |= 1ULL<<v[0]; |
| 81 | a[v[0]] = v[1]; |
| 82 | } |
| 83 | } |
| 84 | |
Rich Felker | 2adf2fb | 2012-01-17 00:34:58 -0500 | [diff] [blame] | 85 | static uint32_t hash(const char *s0) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 86 | { |
Rich Felker | 2adf2fb | 2012-01-17 00:34:58 -0500 | [diff] [blame] | 87 | const unsigned char *s = (void *)s0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 88 | uint_fast32_t h = 0; |
| 89 | while (*s) { |
| 90 | h = 16*h + *s++; |
| 91 | h ^= h>>24 & 0xf0; |
| 92 | } |
| 93 | return h & 0xfffffff; |
| 94 | } |
| 95 | |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 96 | static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 97 | { |
| 98 | size_t i; |
| 99 | for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { |
| 100 | if (!strcmp(s, strings+syms[i].st_name)) |
| 101 | return syms+i; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON) |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 107 | #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 108 | |
| 109 | static void *find_sym(struct dso *dso, const char *s, int need_def) |
| 110 | { |
| 111 | uint32_t h = hash(s); |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 112 | void *def = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 113 | if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1; |
| 114 | if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1; |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame^] | 115 | if (h==0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 116 | for (; dso; dso=dso->next) { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 117 | Sym *sym; |
| 118 | if (!dso->global) continue; |
| 119 | sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 120 | if (sym && (!need_def || sym->st_shndx) && sym->st_value |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 121 | && (1<<(sym->st_info&0xf) & OK_TYPES) |
| 122 | && (1<<(sym->st_info>>4) & OK_BINDS)) { |
Rich Felker | e01ac67 | 2011-07-25 09:22:05 -0400 | [diff] [blame] | 123 | if (def && sym->st_info>>4 == STB_WEAK) continue; |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 124 | def = dso->base + sym->st_value; |
| 125 | if (sym->st_info>>4 == STB_GLOBAL) break; |
| 126 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 127 | } |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 128 | return def; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso) |
| 132 | { |
| 133 | Sym *sym; |
| 134 | const char *name; |
| 135 | size_t sym_val, sym_size; |
| 136 | size_t *reloc_addr; |
| 137 | void *ctx; |
| 138 | int type; |
| 139 | int sym_index; |
| 140 | |
| 141 | for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { |
| 142 | reloc_addr = (void *)(base + rel[0]); |
| 143 | type = R_TYPE(rel[1]); |
| 144 | sym_index = R_SYM(rel[1]); |
| 145 | if (sym_index) { |
| 146 | sym = syms + sym_index; |
| 147 | name = strings + sym->st_name; |
| 148 | ctx = IS_COPY(type) ? dso->next : dso; |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 149 | sym_val = (size_t)find_sym(ctx, name, IS_PLT(type)); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 150 | if (!sym_val && sym->st_info>>4 != STB_WEAK) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 151 | snprintf(errbuf, sizeof errbuf, |
| 152 | "Error relocating %s: %s: symbol not found", |
| 153 | dso->name, name); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 154 | if (runtime) longjmp(rtld_fail, 1); |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 155 | dprintf(2, "%s\n", errbuf); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 156 | _exit(127); |
| 157 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 158 | sym_size = sym->st_size; |
| 159 | } |
| 160 | do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]); |
| 161 | } |
| 162 | } |
| 163 | |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 164 | /* A huge hack: to make up for the wastefulness of shared libraries |
| 165 | * needing at least a page of dirty memory even if they have no global |
| 166 | * data, we reclaim the gaps at the beginning and end of writable maps |
| 167 | * and "donate" them to the heap by setting up minimal malloc |
| 168 | * structures and then freeing them. */ |
| 169 | |
| 170 | static void reclaim(unsigned char *base, size_t start, size_t end) |
| 171 | { |
| 172 | size_t *a, *z; |
| 173 | start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t); |
| 174 | end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t); |
| 175 | if (start>end || end-start < 4*sizeof(size_t)) return; |
| 176 | a = (size_t *)(base + start); |
| 177 | z = (size_t *)(base + end); |
| 178 | a[-2] = 1; |
| 179 | a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1; |
| 180 | z[1] = 1; |
| 181 | free(a); |
| 182 | } |
| 183 | |
| 184 | static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt) |
| 185 | { |
| 186 | for (; phcnt--; ph=(void *)((char *)ph+phent)) { |
| 187 | if (ph->p_type!=PT_LOAD) continue; |
| 188 | if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue; |
| 189 | reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr); |
| 190 | reclaim(base, ph->p_vaddr+ph->p_memsz, |
| 191 | ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE); |
| 192 | } |
| 193 | } |
| 194 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 195 | static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp) |
| 196 | { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 197 | Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 198 | size_t phsize; |
| 199 | size_t addr_min=SIZE_MAX, addr_max=0, map_len; |
| 200 | size_t this_min, this_max; |
| 201 | off_t off_start; |
| 202 | Ehdr *eh; |
| 203 | Phdr *ph; |
| 204 | unsigned prot; |
| 205 | unsigned char *map, *base; |
| 206 | size_t dyn; |
| 207 | size_t i; |
| 208 | |
| 209 | ssize_t l = read(fd, buf, sizeof buf); |
| 210 | if (l<sizeof *eh) return 0; |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 211 | eh = buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 212 | phsize = eh->e_phentsize * eh->e_phnum; |
| 213 | if (phsize + sizeof *eh > l) return 0; |
| 214 | if (eh->e_phoff + phsize > l) { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 215 | l = pread(fd, buf+1, phsize, eh->e_phoff); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 216 | if (l != phsize) return 0; |
| 217 | eh->e_phoff = sizeof *eh; |
| 218 | } |
| 219 | ph = (void *)((char *)buf + eh->e_phoff); |
| 220 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 221 | if (ph->p_type == PT_DYNAMIC) |
| 222 | dyn = ph->p_vaddr; |
| 223 | if (ph->p_type != PT_LOAD) continue; |
| 224 | if (ph->p_vaddr < addr_min) { |
| 225 | addr_min = ph->p_vaddr; |
| 226 | off_start = ph->p_offset; |
| 227 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 228 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 229 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 230 | } |
| 231 | if (ph->p_vaddr+ph->p_memsz > addr_max) { |
| 232 | addr_max = ph->p_vaddr+ph->p_memsz; |
| 233 | } |
| 234 | } |
| 235 | if (!dyn) return 0; |
| 236 | addr_max += PAGE_SIZE-1; |
| 237 | addr_max &= -PAGE_SIZE; |
| 238 | addr_min &= -PAGE_SIZE; |
| 239 | off_start &= -PAGE_SIZE; |
| 240 | map_len = addr_max - addr_min + off_start; |
| 241 | /* The first time, we map too much, possibly even more than |
| 242 | * the length of the file. This is okay because we will not |
| 243 | * use the invalid part; we just need to reserve the right |
| 244 | * amount of virtual address space to map over later. */ |
Rich Felker | bf30100 | 2011-06-28 14:20:41 -0400 | [diff] [blame] | 245 | 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] | 246 | if (map==MAP_FAILED) return 0; |
| 247 | base = map - addr_min; |
| 248 | ph = (void *)((char *)buf + eh->e_phoff); |
| 249 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 250 | if (ph->p_type != PT_LOAD) continue; |
| 251 | /* Reuse the existing mapping for the lowest-address LOAD */ |
| 252 | if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue; |
| 253 | this_min = ph->p_vaddr & -PAGE_SIZE; |
| 254 | this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; |
| 255 | off_start = ph->p_offset & -PAGE_SIZE; |
| 256 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 257 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 258 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 259 | if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) { |
| 260 | munmap(map, map_len); |
| 261 | return 0; |
| 262 | } |
| 263 | if (ph->p_memsz > ph->p_filesz) { |
| 264 | size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; |
| 265 | size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; |
| 266 | memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); |
| 267 | 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) { |
| 268 | munmap(map, map_len); |
| 269 | return 0; |
| 270 | } |
| 271 | } |
| 272 | } |
Rich Felker | 9f17413 | 2011-06-29 00:29:08 -0400 | [diff] [blame] | 273 | for (i=0; ((size_t *)(base+dyn))[i]; i+=2) |
| 274 | if (((size_t *)(base+dyn))[i]==DT_TEXTREL) { |
| 275 | mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC); |
| 276 | break; |
| 277 | } |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 278 | if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff), |
| 279 | eh->e_phentsize, eh->e_phnum); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 280 | *lenp = map_len; |
| 281 | *basep = base; |
| 282 | *dynp = dyn; |
| 283 | return map; |
| 284 | } |
| 285 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 286 | static int path_open(const char *name, const char *search) |
| 287 | { |
| 288 | char buf[2*NAME_MAX+2]; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 289 | const char *s=search, *z; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 290 | int l, fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 291 | for (;;) { |
| 292 | while (*s==':') s++; |
| 293 | if (!*s) return -1; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 294 | z = strchr(s, ':'); |
| 295 | l = z ? z-s : strlen(s); |
| 296 | snprintf(buf, sizeof buf, "%.*s/%s", l, s, name); |
| 297 | if ((fd = open(buf, O_RDONLY))>=0) return fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 298 | s += l; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 299 | } |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 302 | static void decode_dyn(struct dso *p) |
| 303 | { |
| 304 | size_t dyn[DYN_CNT] = {0}; |
| 305 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 306 | p->syms = (void *)(p->base + dyn[DT_SYMTAB]); |
| 307 | p->hashtab = (void *)(p->base + dyn[DT_HASH]); |
| 308 | p->strings = (void *)(p->base + dyn[DT_STRTAB]); |
| 309 | } |
| 310 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 311 | static struct dso *load_library(const char *name) |
| 312 | { |
| 313 | unsigned char *base, *map; |
| 314 | size_t dyno, map_len; |
| 315 | struct dso *p; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 316 | int fd; |
| 317 | struct stat st; |
| 318 | |
| 319 | /* Catch and block attempts to reload the implementation itself */ |
| 320 | if (name[0]=='l' && name[1]=='i' && name[2]=='b') { |
| 321 | static const char *rp, reserved[] = |
| 322 | "c\0pthread\0rt\0m\0dl\0util\0xnet\0"; |
| 323 | char *z = strchr(name, '.'); |
| 324 | if (z) { |
| 325 | size_t l = z-name; |
| 326 | for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1); |
| 327 | if (*rp) { |
| 328 | if (!libc->prev) { |
| 329 | tail->next = libc; |
| 330 | libc->prev = tail; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 331 | tail = libc->next ? libc->next : libc; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 332 | } |
| 333 | return libc; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | /* Search for the name to see if it's already loaded */ |
| 338 | for (p=head->next; p; p=p->next) { |
| 339 | if (!strcmp(p->name, name)) { |
| 340 | p->refcnt++; |
| 341 | return p; |
| 342 | } |
| 343 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 344 | if (strchr(name, '/')) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 345 | fd = open(name, O_RDONLY); |
| 346 | } else { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 347 | if (strlen(name) > NAME_MAX) return 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 348 | fd = -1; |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 349 | if (r_path) fd = path_open(name, r_path); |
| 350 | if (fd < 0 && env_path) fd = path_open(name, env_path); |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 351 | if (fd < 0) { |
| 352 | if (!sys_path) { |
| 353 | FILE *f = fopen(ETC_LDSO_PATH, "r"); |
| 354 | if (f) { |
| 355 | if (getline(&sys_path, (size_t[1]){0}, f) > 0) |
| 356 | sys_path[strlen(sys_path)-1]=0; |
| 357 | fclose(f); |
| 358 | } |
| 359 | } |
| 360 | if (sys_path) fd = path_open(name, sys_path); |
| 361 | else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib"); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | if (fd < 0) return 0; |
| 365 | if (fstat(fd, &st) < 0) { |
| 366 | close(fd); |
| 367 | return 0; |
| 368 | } |
| 369 | for (p=head->next; p; p=p->next) { |
| 370 | if (p->dev == st.st_dev && p->ino == st.st_ino) { |
| 371 | close(fd); |
| 372 | p->refcnt++; |
| 373 | return p; |
| 374 | } |
| 375 | } |
| 376 | map = map_library(fd, &map_len, &base, &dyno); |
| 377 | close(fd); |
| 378 | if (!map) return 0; |
| 379 | p = calloc(1, sizeof *p + strlen(name) + 1); |
| 380 | if (!p) { |
| 381 | munmap(map, map_len); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | p->map = map; |
| 386 | p->map_len = map_len; |
| 387 | p->base = base; |
| 388 | p->dynv = (void *)(base + dyno); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 389 | decode_dyn(p); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 390 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 391 | p->dev = st.st_dev; |
| 392 | p->ino = st.st_ino; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 393 | p->refcnt = 1; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 394 | p->name = p->buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 395 | strcpy(p->name, name); |
| 396 | |
| 397 | tail->next = p; |
| 398 | p->prev = tail; |
| 399 | tail = p; |
| 400 | |
| 401 | return p; |
| 402 | } |
| 403 | |
| 404 | static void load_deps(struct dso *p) |
| 405 | { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 406 | size_t i, ndeps=0; |
| 407 | struct dso ***deps = &p->deps, **tmp, *dep; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 408 | for (; p; p=p->next) { |
| 409 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 410 | if (p->dynv[i] != DT_RPATH) continue; |
| 411 | r_path = (void *)(p->strings + p->dynv[i+1]); |
| 412 | } |
| 413 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 414 | if (p->dynv[i] != DT_NEEDED) continue; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 415 | dep = load_library(p->strings + p->dynv[i+1]); |
| 416 | if (!dep) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 417 | snprintf(errbuf, sizeof errbuf, |
| 418 | "Error loading shared library %s: %m (needed by %s)", |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 419 | p->strings + p->dynv[i+1], p->name); |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 420 | if (runtime) longjmp(rtld_fail, 1); |
| 421 | dprintf(2, "%s\n", errbuf); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 422 | _exit(127); |
| 423 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 424 | if (runtime) { |
| 425 | tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2)); |
| 426 | if (!tmp) longjmp(rtld_fail, 1); |
| 427 | tmp[ndeps++] = dep; |
| 428 | tmp[ndeps] = 0; |
| 429 | *deps = tmp; |
| 430 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 431 | } |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 432 | r_path = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 436 | static void load_preload(char *s) |
| 437 | { |
| 438 | int tmp; |
| 439 | char *z; |
| 440 | for (z=s; *z; s=z) { |
| 441 | for ( ; *s && isspace(*s); s++); |
| 442 | for (z=s; *z && !isspace(*z); z++); |
| 443 | tmp = *z; |
| 444 | *z = 0; |
| 445 | load_library(s); |
| 446 | *z = tmp; |
| 447 | } |
| 448 | } |
| 449 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 450 | static void make_global(struct dso *p) |
| 451 | { |
| 452 | for (; p; p=p->next) p->global = 1; |
| 453 | } |
| 454 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 455 | static void reloc_all(struct dso *p) |
| 456 | { |
| 457 | size_t dyn[DYN_CNT] = {0}; |
| 458 | for (; p; p=p->next) { |
| 459 | if (p->relocated) continue; |
| 460 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 461 | do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 462 | 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 463 | do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 464 | 2, p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 465 | do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 466 | 3, p->syms, p->strings, head); |
Rich Felker | 368ba4a | 2011-06-25 00:18:19 -0400 | [diff] [blame] | 467 | p->relocated = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 471 | static void free_all(struct dso *p) |
| 472 | { |
| 473 | struct dso *n; |
| 474 | while (p) { |
| 475 | n = p->next; |
| 476 | if (p->map) free(p); |
| 477 | p = n; |
| 478 | } |
| 479 | } |
| 480 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 481 | static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride) |
| 482 | { |
| 483 | for (; cnt--; ph = (void *)((char *)ph + stride)) |
| 484 | if (ph->p_type == PT_DYNAMIC) |
| 485 | return ph->p_vaddr; |
| 486 | return 0; |
| 487 | } |
| 488 | |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 489 | static void do_init_fini(struct dso *p) |
| 490 | { |
| 491 | size_t dyn[DYN_CNT] = {0}; |
| 492 | for (; p; p=p->prev) { |
| 493 | if (p->constructed) return; |
| 494 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 495 | if (dyn[0] & (1<<DT_FINI)) |
| 496 | atexit((void (*)(void))(p->base + dyn[DT_FINI])); |
| 497 | if (dyn[0] & (1<<DT_INIT)) |
| 498 | ((void (*)(void))(p->base + dyn[DT_INIT]))(); |
| 499 | p->constructed = 1; |
| 500 | } |
| 501 | } |
| 502 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 503 | void *__dynlink(int argc, char **argv) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 504 | { |
| 505 | size_t *auxv, aux[AUX_CNT] = {0}; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 506 | size_t i; |
| 507 | Phdr *phdr; |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 508 | Ehdr *ehdr; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 509 | static struct dso builtin_dsos[3]; |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 510 | struct dso *const app = builtin_dsos+0; |
| 511 | struct dso *const lib = builtin_dsos+1; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 512 | struct dso *const vdso = builtin_dsos+2; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 513 | char *env_preload=0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 514 | |
| 515 | /* Find aux vector just past environ[] */ |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 516 | for (i=argc+1; argv[i]; i++) |
| 517 | if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16)) |
| 518 | env_path = argv[i]+16; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 519 | else if (!memcmp(argv[i], "LD_PRELOAD=", 11)) |
| 520 | env_preload = argv[i]+11; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 521 | auxv = (void *)(argv+i+1); |
| 522 | |
| 523 | decode_vec(auxv, aux, AUX_CNT); |
| 524 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 525 | /* Only trust user/env if kernel says we're not suid/sgid */ |
| 526 | if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] |
Rich Felker | a045883 | 2011-08-16 07:46:42 -0400 | [diff] [blame] | 527 | || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) { |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 528 | env_path = 0; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 529 | env_preload = 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 530 | } |
| 531 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 532 | /* The dynamic linker load address is passed by the kernel |
| 533 | * in the AUX vector, so this is easy. */ |
| 534 | lib->base = (void *)aux[AT_BASE]; |
| 535 | lib->name = "libc.so"; |
| 536 | lib->global = 1; |
| 537 | ehdr = (void *)lib->base; |
| 538 | lib->dynv = (void *)(lib->base + find_dyn( |
| 539 | (void *)(aux[AT_BASE]+ehdr->e_phoff), |
| 540 | ehdr->e_phnum, ehdr->e_phentsize)); |
| 541 | decode_dyn(lib); |
| 542 | |
Rich Felker | e12fe65 | 2012-01-23 02:02:59 -0500 | [diff] [blame] | 543 | /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */ |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 544 | app->base = 0; |
Rich Felker | e12fe65 | 2012-01-23 02:02:59 -0500 | [diff] [blame] | 545 | phdr = (void *)aux[AT_PHDR]; |
| 546 | for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) { |
| 547 | if (phdr->p_type == PT_PHDR) |
| 548 | app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr); |
| 549 | } |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 550 | app->name = argv[0]; |
| 551 | app->global = 1; |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 552 | app->constructed = 1; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 553 | app->dynv = (void *)(app->base + find_dyn( |
| 554 | (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT])); |
| 555 | decode_dyn(app); |
| 556 | |
| 557 | /* Attach to vdso, if provided by the kernel */ |
Rich Felker | cf8506a | 2011-08-16 08:50:03 -0400 | [diff] [blame] | 558 | for (i=0; auxv[i]; i+=2) { |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 559 | size_t vdso_base = auxv[i+1]; |
| 560 | if (auxv[i] != AT_SYSINFO_EHDR) continue; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 561 | ehdr = (void *)vdso_base; |
| 562 | phdr = (void *)(vdso_base + ehdr->e_phoff); |
| 563 | for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) { |
| 564 | if (phdr->p_type == PT_DYNAMIC) |
| 565 | vdso->dynv = (void *)(vdso_base + phdr->p_offset); |
| 566 | if (phdr->p_type == PT_LOAD) |
| 567 | vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset); |
| 568 | } |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 569 | vdso->name = "linux-gate.so.1"; |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 570 | vdso->global = 1; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 571 | decode_dyn(vdso); |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 572 | vdso->prev = lib; |
| 573 | lib->next = vdso; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 574 | break; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 575 | } |
| 576 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 577 | /* Initial dso chain consists only of the app. We temporarily |
| 578 | * append the dynamic linker/libc so we can relocate it, then |
| 579 | * restore the initial chain in preparation for loading third |
| 580 | * party libraries (preload/needed). */ |
| 581 | head = tail = app; |
| 582 | libc = lib; |
| 583 | app->next = lib; |
| 584 | reloc_all(lib); |
| 585 | app->next = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 586 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 587 | /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */ |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 588 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 589 | /* Donate unused parts of app and library mapping to malloc */ |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 590 | reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]); |
| 591 | ehdr = (void *)lib->base; |
| 592 | reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff), |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 593 | ehdr->e_phentsize, ehdr->e_phnum); |
| 594 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 595 | /* Load preload/needed libraries, add their symbols to the global |
Rich Felker | fd7015d | 2012-01-23 18:32:40 -0500 | [diff] [blame] | 596 | * namespace, and perform all remaining relocations. The main |
| 597 | * program must be relocated LAST since it may contain copy |
| 598 | * relocations which depend on libraries' relocations. */ |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 599 | if (env_preload) load_preload(env_preload); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 600 | load_deps(app); |
| 601 | make_global(app); |
Rich Felker | fd7015d | 2012-01-23 18:32:40 -0500 | [diff] [blame] | 602 | reloc_all(app->next); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 603 | reloc_all(app); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 604 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 605 | /* Switch to runtime mode: any further failures in the dynamic |
| 606 | * linker are a reportable failure rather than a fatal startup |
| 607 | * error. If the dynamic loader (dlopen) will not be used, free |
| 608 | * all memory used by the dynamic linker. */ |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 609 | runtime = 1; |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 610 | |
| 611 | do_init_fini(tail); |
| 612 | |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 613 | if (!rtld_used) { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 614 | free_all(head); |
| 615 | free(sys_path); |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 616 | reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 617 | } |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 618 | |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame^] | 619 | if (ssp_used) __pthread_self_init(); |
| 620 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 621 | errno = 0; |
| 622 | return (void *)aux[AT_ENTRY]; |
| 623 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 624 | |
| 625 | void *dlopen(const char *file, int mode) |
| 626 | { |
Rich Felker | 2fdea17 | 2011-07-01 22:40:00 -0400 | [diff] [blame] | 627 | struct dso *volatile p, *orig_tail = tail, *next; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 628 | size_t i; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 629 | int cs; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 630 | |
| 631 | if (!file) return head; |
| 632 | |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 633 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 634 | pthread_rwlock_wrlock(&lock); |
| 635 | |
| 636 | if (setjmp(rtld_fail)) { |
| 637 | /* Clean up anything new that was (partially) loaded */ |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 638 | if (p->deps) for (i=0; p->deps[i]; i++) |
| 639 | if (p->deps[i]->global < 0) |
| 640 | p->deps[i]->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 641 | for (p=orig_tail->next; p; p=next) { |
| 642 | next = p->next; |
| 643 | munmap(p->map, p->map_len); |
| 644 | free(p->deps); |
| 645 | free(p); |
| 646 | } |
| 647 | tail = orig_tail; |
| 648 | tail->next = 0; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 649 | p = 0; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 650 | errflag = 1; |
| 651 | goto end; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 652 | } else p = load_library(file); |
| 653 | |
| 654 | if (!p) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 655 | snprintf(errbuf, sizeof errbuf, |
| 656 | "Error loading shared library %s: %m", file); |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 657 | errflag = 1; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 658 | goto end; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 659 | } |
| 660 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 661 | /* First load handling */ |
| 662 | if (!p->deps) { |
| 663 | load_deps(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 664 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 665 | if (!p->deps[i]->global) |
| 666 | p->deps[i]->global = -1; |
| 667 | if (!p->global) p->global = -1; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 668 | reloc_all(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 669 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 670 | if (p->deps[i]->global < 0) |
| 671 | p->deps[i]->global = 0; |
| 672 | if (p->global < 0) p->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | if (mode & RTLD_GLOBAL) { |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 676 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 677 | p->deps[i]->global = 1; |
| 678 | p->global = 1; |
| 679 | } |
| 680 | |
Rich Felker | ce4d97e | 2012-02-06 17:57:29 -0500 | [diff] [blame] | 681 | do_init_fini(tail); |
Rich Felker | 06933cc | 2011-06-26 22:09:32 -0400 | [diff] [blame] | 682 | end: |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 683 | pthread_rwlock_unlock(&lock); |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 684 | pthread_setcancelstate(cs, 0); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 685 | return p; |
| 686 | } |
| 687 | |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 688 | static void *do_dlsym(struct dso *p, const char *s, void *ra) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 689 | { |
| 690 | size_t i; |
| 691 | uint32_t h; |
| 692 | Sym *sym; |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 693 | if (p == RTLD_NEXT) { |
| 694 | for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next); |
| 695 | if (!p) p=head; |
| 696 | p=p->next; |
| 697 | } |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 698 | if (p == head || p == RTLD_DEFAULT) { |
| 699 | void *res = find_sym(head, s, 0); |
| 700 | if (!res) errflag = 1; |
| 701 | return res; |
| 702 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 703 | h = hash(s); |
| 704 | sym = lookup(s, h, p->syms, p->hashtab, p->strings); |
| 705 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 706 | return p->base + sym->st_value; |
| 707 | if (p->deps) for (i=0; p->deps[i]; i++) { |
| 708 | sym = lookup(s, h, p->deps[i]->syms, |
| 709 | p->deps[i]->hashtab, p->deps[i]->strings); |
| 710 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 711 | return p->deps[i]->base + sym->st_value; |
| 712 | } |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 713 | errflag = 1; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 714 | snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 718 | void *__dlsym(void *p, const char *s, void *ra) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 719 | { |
| 720 | void *res; |
| 721 | pthread_rwlock_rdlock(&lock); |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 722 | res = do_dlsym(p, s, ra); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 723 | pthread_rwlock_unlock(&lock); |
| 724 | return res; |
| 725 | } |
Rich Felker | 5a09a53 | 2012-02-03 03:16:07 -0500 | [diff] [blame] | 726 | #else |
| 727 | void *dlopen(const char *file, int mode) |
| 728 | { |
| 729 | return 0; |
| 730 | } |
| 731 | void *__dlsym(void *p, const char *s, void *ra) |
| 732 | { |
| 733 | return 0; |
| 734 | } |
| 735 | #endif |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 736 | |
| 737 | char *dlerror() |
| 738 | { |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 739 | if (!errflag) return 0; |
| 740 | errflag = 0; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 741 | return errbuf; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | int dlclose(void *p) |
| 745 | { |
| 746 | return 0; |
| 747 | } |