blob: a171df977196c708c65819d5de885112d2f98644 [file] [log] [blame]
Rich Felker51e2d832011-06-18 19:48:42 -04001#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 Felker6b3d5e52011-06-26 17:39:17 -040015#include <setjmp.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040016#include <pthread.h>
Rich Felker2719cc82011-08-16 00:24:36 -040017#include <ctype.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040018#include <dlfcn.h>
Rich Felker51e2d832011-06-18 19:48:42 -040019
Rich Felkera9e85c02012-03-23 00:28:20 -040020static int errflag;
Rich Felkera5d10eb2012-04-23 12:03:31 -040021static char errbuf[128];
Rich Felkera9e85c02012-03-23 00:28:20 -040022
Rich Felkere864a292012-07-11 01:47:30 -040023#ifdef SHARED
Rich Felkera9e85c02012-03-23 00:28:20 -040024
Rich Felker51e2d832011-06-18 19:48:42 -040025#include "reloc.h"
26
27#if ULONG_MAX == 0xffffffff
28typedef Elf32_Ehdr Ehdr;
29typedef Elf32_Phdr Phdr;
30typedef Elf32_Sym Sym;
31#define R_TYPE(x) ((x)&255)
32#define R_SYM(x) ((x)>>8)
33#else
34typedef Elf64_Ehdr Ehdr;
35typedef Elf64_Phdr Phdr;
36typedef Elf64_Sym Sym;
37#define R_TYPE(x) ((x)&0xffffffff)
38#define R_SYM(x) ((x)>>32)
39#endif
40
Rich Felker3ec8d292012-04-25 00:05:42 -040041struct debug {
42 int ver;
43 void *head;
44 void (*bp)(void);
45 int state;
46 void *base;
47};
48
49struct dso {
50 unsigned char *base;
51 char *name;
Rich Felker51e2d832011-06-18 19:48:42 -040052 size_t *dynv;
Rich Felker3ec8d292012-04-25 00:05:42 -040053 struct dso *next, *prev;
54
55 int refcnt;
Rich Felker51e2d832011-06-18 19:48:42 -040056 Sym *syms;
Rich Felker596d60c2011-06-18 22:52:01 -040057 uint32_t *hashtab;
Rich Felker51e2d832011-06-18 19:48:42 -040058 char *strings;
Rich Felker51e2d832011-06-18 19:48:42 -040059 unsigned char *map;
60 size_t map_len;
61 dev_t dev;
62 ino_t ino;
Rich Felker6343ac82012-06-09 21:20:44 -040063 signed char global;
Rich Felker700a8152012-02-07 20:29:29 -050064 char relocated;
65 char constructed;
Rich Felker59ab43f2011-06-26 19:23:28 -040066 struct dso **deps;
Rich Felker5c1909a2012-05-27 16:01:44 -040067 char *shortname;
Rich Felker6b3d5e52011-06-26 17:39:17 -040068 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040069};
70
Rich Felker58aa5f42012-05-03 20:42:45 -040071void __init_ssp(size_t *);
Rich Felker60872cf2012-04-24 18:07:59 -040072
Rich Felker51e2d832011-06-18 19:48:42 -040073static struct dso *head, *tail, *libc;
Rich Felker191ebca2011-06-30 23:02:27 -040074static char *env_path, *sys_path, *r_path;
Rich Felker59ab43f2011-06-26 19:23:28 -040075static int rtld_used;
Rich Felker60872cf2012-04-24 18:07:59 -040076static int ssp_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -040077static int runtime;
Rich Felker5c1909a2012-05-27 16:01:44 -040078static int ldd_mode;
Rich Felker6b3d5e52011-06-26 17:39:17 -040079static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -040080static pthread_rwlock_t lock;
Rich Felker3ec8d292012-04-25 00:05:42 -040081static struct debug debug;
82
83struct debug *_dl_debug_addr = &debug;
Rich Felker51e2d832011-06-18 19:48:42 -040084
Rich Felkera0458832011-08-16 07:46:42 -040085#define AUX_CNT 24
Rich Felker51e2d832011-06-18 19:48:42 -040086#define DYN_CNT 34
87
88static void decode_vec(size_t *v, size_t *a, size_t cnt)
89{
90 memset(a, 0, cnt*sizeof(size_t));
91 for (; v[0]; v+=2) if (v[0]<cnt) {
92 a[0] |= 1ULL<<v[0];
93 a[v[0]] = v[1];
94 }
95}
96
Rich Felker2adf2fb2012-01-17 00:34:58 -050097static uint32_t hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -040098{
Rich Felker2adf2fb2012-01-17 00:34:58 -050099 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -0400100 uint_fast32_t h = 0;
101 while (*s) {
102 h = 16*h + *s++;
103 h ^= h>>24 & 0xf0;
104 }
105 return h & 0xfffffff;
106}
107
Rich Felker596d60c2011-06-18 22:52:01 -0400108static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings)
Rich Felker51e2d832011-06-18 19:48:42 -0400109{
110 size_t i;
111 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
112 if (!strcmp(s, strings+syms[i].st_name))
113 return syms+i;
114 }
115 return 0;
116}
117
118#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
Rich Felker427173b2011-07-24 02:19:47 -0400119#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -0400120
121static void *find_sym(struct dso *dso, const char *s, int need_def)
122{
123 uint32_t h = hash(s);
Rich Felker427173b2011-07-24 02:19:47 -0400124 void *def = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400125 if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
126 if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
Rich Felker60872cf2012-04-24 18:07:59 -0400127 if (h==0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400128 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400129 Sym *sym;
130 if (!dso->global) continue;
131 sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings);
Rich Felker51e2d832011-06-18 19:48:42 -0400132 if (sym && (!need_def || sym->st_shndx) && sym->st_value
Rich Felker427173b2011-07-24 02:19:47 -0400133 && (1<<(sym->st_info&0xf) & OK_TYPES)
134 && (1<<(sym->st_info>>4) & OK_BINDS)) {
Rich Felkere01ac672011-07-25 09:22:05 -0400135 if (def && sym->st_info>>4 == STB_WEAK) continue;
Rich Felker427173b2011-07-24 02:19:47 -0400136 def = dso->base + sym->st_value;
137 if (sym->st_info>>4 == STB_GLOBAL) break;
138 }
Rich Felker51e2d832011-06-18 19:48:42 -0400139 }
Rich Felker427173b2011-07-24 02:19:47 -0400140 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400141}
142
143static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso)
144{
145 Sym *sym;
146 const char *name;
147 size_t sym_val, sym_size;
148 size_t *reloc_addr;
149 void *ctx;
150 int type;
151 int sym_index;
152
153 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
154 reloc_addr = (void *)(base + rel[0]);
155 type = R_TYPE(rel[1]);
156 sym_index = R_SYM(rel[1]);
157 if (sym_index) {
158 sym = syms + sym_index;
159 name = strings + sym->st_name;
160 ctx = IS_COPY(type) ? dso->next : dso;
Rich Felker32de61e2011-06-25 22:36:21 -0400161 sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
Rich Felker6b3d5e52011-06-26 17:39:17 -0400162 if (!sym_val && sym->st_info>>4 != STB_WEAK) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400163 snprintf(errbuf, sizeof errbuf,
164 "Error relocating %s: %s: symbol not found",
165 dso->name, name);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400166 if (runtime) longjmp(rtld_fail, 1);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400167 dprintf(2, "%s\n", errbuf);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400168 _exit(127);
169 }
Rich Felker51e2d832011-06-18 19:48:42 -0400170 sym_size = sym->st_size;
171 }
172 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
173 }
174}
175
Rich Felker6717e622011-06-28 19:40:14 -0400176/* A huge hack: to make up for the wastefulness of shared libraries
177 * needing at least a page of dirty memory even if they have no global
178 * data, we reclaim the gaps at the beginning and end of writable maps
179 * and "donate" them to the heap by setting up minimal malloc
180 * structures and then freeing them. */
181
182static void reclaim(unsigned char *base, size_t start, size_t end)
183{
184 size_t *a, *z;
185 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
186 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
187 if (start>end || end-start < 4*sizeof(size_t)) return;
188 a = (size_t *)(base + start);
189 z = (size_t *)(base + end);
190 a[-2] = 1;
191 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
192 z[1] = 1;
193 free(a);
194}
195
196static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
197{
198 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
199 if (ph->p_type!=PT_LOAD) continue;
200 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
201 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
202 reclaim(base, ph->p_vaddr+ph->p_memsz,
203 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
204 }
205}
206
Rich Felker51e2d832011-06-18 19:48:42 -0400207static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
208{
Rich Felker59633c72011-06-25 12:26:08 -0400209 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400210 size_t phsize;
211 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
212 size_t this_min, this_max;
213 off_t off_start;
214 Ehdr *eh;
215 Phdr *ph;
216 unsigned prot;
217 unsigned char *map, *base;
218 size_t dyn;
219 size_t i;
220
221 ssize_t l = read(fd, buf, sizeof buf);
222 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400223 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400224 phsize = eh->e_phentsize * eh->e_phnum;
225 if (phsize + sizeof *eh > l) return 0;
226 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400227 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400228 if (l != phsize) return 0;
229 eh->e_phoff = sizeof *eh;
230 }
231 ph = (void *)((char *)buf + eh->e_phoff);
232 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
233 if (ph->p_type == PT_DYNAMIC)
234 dyn = ph->p_vaddr;
235 if (ph->p_type != PT_LOAD) continue;
236 if (ph->p_vaddr < addr_min) {
237 addr_min = ph->p_vaddr;
238 off_start = ph->p_offset;
239 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
240 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
241 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
242 }
243 if (ph->p_vaddr+ph->p_memsz > addr_max) {
244 addr_max = ph->p_vaddr+ph->p_memsz;
245 }
246 }
247 if (!dyn) return 0;
248 addr_max += PAGE_SIZE-1;
249 addr_max &= -PAGE_SIZE;
250 addr_min &= -PAGE_SIZE;
251 off_start &= -PAGE_SIZE;
252 map_len = addr_max - addr_min + off_start;
253 /* The first time, we map too much, possibly even more than
254 * the length of the file. This is okay because we will not
255 * use the invalid part; we just need to reserve the right
256 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400257 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400258 if (map==MAP_FAILED) return 0;
259 base = map - addr_min;
260 ph = (void *)((char *)buf + eh->e_phoff);
261 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
262 if (ph->p_type != PT_LOAD) continue;
263 /* Reuse the existing mapping for the lowest-address LOAD */
264 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
265 this_min = ph->p_vaddr & -PAGE_SIZE;
266 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
267 off_start = ph->p_offset & -PAGE_SIZE;
268 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
269 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
270 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400271 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
272 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400273 if (ph->p_memsz > ph->p_filesz) {
274 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
275 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
276 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400277 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)
278 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400279 }
280 }
Rich Felker9f174132011-06-29 00:29:08 -0400281 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
282 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400283 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
284 goto error;
Rich Felker9f174132011-06-29 00:29:08 -0400285 break;
286 }
Rich Felker6717e622011-06-28 19:40:14 -0400287 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
288 eh->e_phentsize, eh->e_phnum);
Rich Felker51e2d832011-06-18 19:48:42 -0400289 *lenp = map_len;
290 *basep = base;
291 *dynp = dyn;
292 return map;
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400293error:
294 munmap(map, map_len);
295 return 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400296}
297
Rich Felker5c1909a2012-05-27 16:01:44 -0400298static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
Rich Felker568b8072011-06-25 01:56:34 -0400299{
Rich Felker49388f32011-06-25 17:49:16 -0400300 const char *s=search, *z;
Rich Felker568b8072011-06-25 01:56:34 -0400301 int l, fd;
Rich Felker49388f32011-06-25 17:49:16 -0400302 for (;;) {
303 while (*s==':') s++;
304 if (!*s) return -1;
Rich Felker568b8072011-06-25 01:56:34 -0400305 z = strchr(s, ':');
306 l = z ? z-s : strlen(s);
Rich Felker5c1909a2012-05-27 16:01:44 -0400307 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
Rich Felker568b8072011-06-25 01:56:34 -0400308 if ((fd = open(buf, O_RDONLY))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400309 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400310 }
Rich Felker568b8072011-06-25 01:56:34 -0400311}
312
Rich Felkerc82f4a32012-01-23 00:57:38 -0500313static void decode_dyn(struct dso *p)
314{
315 size_t dyn[DYN_CNT] = {0};
316 decode_vec(p->dynv, dyn, DYN_CNT);
317 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
318 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
319 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
320}
321
Rich Felker51e2d832011-06-18 19:48:42 -0400322static struct dso *load_library(const char *name)
323{
Rich Felker5c1909a2012-05-27 16:01:44 -0400324 char buf[2*NAME_MAX+2];
Rich Felker0420b872012-07-11 01:41:20 -0400325 const char *pathname;
Rich Felker51e2d832011-06-18 19:48:42 -0400326 unsigned char *base, *map;
327 size_t dyno, map_len;
328 struct dso *p;
Rich Felker51e2d832011-06-18 19:48:42 -0400329 int fd;
330 struct stat st;
331
332 /* Catch and block attempts to reload the implementation itself */
333 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
334 static const char *rp, reserved[] =
335 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
336 char *z = strchr(name, '.');
337 if (z) {
338 size_t l = z-name;
339 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
340 if (*rp) {
341 if (!libc->prev) {
342 tail->next = libc;
343 libc->prev = tail;
Rich Felker6ab444d2011-07-24 00:54:55 -0400344 tail = libc->next ? libc->next : libc;
Rich Felker51e2d832011-06-18 19:48:42 -0400345 }
346 return libc;
347 }
348 }
349 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400350 if (strchr(name, '/')) {
Rich Felker0420b872012-07-11 01:41:20 -0400351 pathname = name;
Rich Felker51e2d832011-06-18 19:48:42 -0400352 fd = open(name, O_RDONLY);
353 } else {
Rich Felker0420b872012-07-11 01:41:20 -0400354 /* Search for the name to see if it's already loaded */
355 for (p=head->next; p; p=p->next) {
356 if (p->shortname && !strcmp(p->shortname, name)) {
357 p->refcnt++;
358 return p;
359 }
360 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400361 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400362 fd = -1;
Rich Felker5c1909a2012-05-27 16:01:44 -0400363 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
364 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
Rich Felker568b8072011-06-25 01:56:34 -0400365 if (fd < 0) {
366 if (!sys_path) {
367 FILE *f = fopen(ETC_LDSO_PATH, "r");
368 if (f) {
369 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
370 sys_path[strlen(sys_path)-1]=0;
371 fclose(f);
372 }
373 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400374 if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf);
375 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf);
Rich Felker51e2d832011-06-18 19:48:42 -0400376 }
Rich Felker0420b872012-07-11 01:41:20 -0400377 pathname = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400378 }
379 if (fd < 0) return 0;
380 if (fstat(fd, &st) < 0) {
381 close(fd);
382 return 0;
383 }
384 for (p=head->next; p; p=p->next) {
385 if (p->dev == st.st_dev && p->ino == st.st_ino) {
Rich Felker0420b872012-07-11 01:41:20 -0400386 /* If this library was previously loaded with a
387 * pathname but a search found the same inode,
388 * setup its shortname so it can be found by name. */
389 if (!p->shortname) p->shortname = strrchr(p->name, '/')+1;
Rich Felker51e2d832011-06-18 19:48:42 -0400390 close(fd);
391 p->refcnt++;
392 return p;
393 }
394 }
395 map = map_library(fd, &map_len, &base, &dyno);
396 close(fd);
397 if (!map) return 0;
Rich Felker0420b872012-07-11 01:41:20 -0400398 p = calloc(1, sizeof *p + strlen(pathname) + 1);
Rich Felker51e2d832011-06-18 19:48:42 -0400399 if (!p) {
400 munmap(map, map_len);
401 return 0;
402 }
403
404 p->map = map;
405 p->map_len = map_len;
406 p->base = base;
407 p->dynv = (void *)(base + dyno);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500408 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400409
Rich Felker51e2d832011-06-18 19:48:42 -0400410 p->dev = st.st_dev;
411 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400412 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400413 p->name = p->buf;
Rich Felker0420b872012-07-11 01:41:20 -0400414 strcpy(p->name, pathname);
415 /* Add a shortname only if name arg was not an explicit pathname. */
416 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
Rich Felker51e2d832011-06-18 19:48:42 -0400417
418 tail->next = p;
419 p->prev = tail;
420 tail = p;
421
Rich Felker0420b872012-07-11 01:41:20 -0400422 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base);
Rich Felker5c1909a2012-05-27 16:01:44 -0400423
Rich Felker51e2d832011-06-18 19:48:42 -0400424 return p;
425}
426
427static void load_deps(struct dso *p)
428{
Rich Felker59ab43f2011-06-26 19:23:28 -0400429 size_t i, ndeps=0;
430 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400431 for (; p; p=p->next) {
432 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400433 if (p->dynv[i] != DT_RPATH) continue;
434 r_path = (void *)(p->strings + p->dynv[i+1]);
435 }
436 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400437 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400438 dep = load_library(p->strings + p->dynv[i+1]);
439 if (!dep) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400440 snprintf(errbuf, sizeof errbuf,
441 "Error loading shared library %s: %m (needed by %s)",
Rich Felker6b3d5e52011-06-26 17:39:17 -0400442 p->strings + p->dynv[i+1], p->name);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400443 if (runtime) longjmp(rtld_fail, 1);
444 dprintf(2, "%s\n", errbuf);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400445 _exit(127);
446 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400447 if (runtime) {
448 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
449 if (!tmp) longjmp(rtld_fail, 1);
450 tmp[ndeps++] = dep;
451 tmp[ndeps] = 0;
452 *deps = tmp;
453 }
Rich Felker51e2d832011-06-18 19:48:42 -0400454 }
Rich Felker191ebca2011-06-30 23:02:27 -0400455 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400456 }
457}
458
Rich Felker2719cc82011-08-16 00:24:36 -0400459static void load_preload(char *s)
460{
461 int tmp;
462 char *z;
463 for (z=s; *z; s=z) {
464 for ( ; *s && isspace(*s); s++);
465 for (z=s; *z && !isspace(*z); z++);
466 tmp = *z;
467 *z = 0;
468 load_library(s);
469 *z = tmp;
470 }
471}
472
Rich Felker59ab43f2011-06-26 19:23:28 -0400473static void make_global(struct dso *p)
474{
475 for (; p; p=p->next) p->global = 1;
476}
477
Rich Felker51e2d832011-06-18 19:48:42 -0400478static void reloc_all(struct dso *p)
479{
480 size_t dyn[DYN_CNT] = {0};
481 for (; p; p=p->next) {
482 if (p->relocated) continue;
483 decode_vec(p->dynv, dyn, DYN_CNT);
484 do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400485 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400486 do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400487 2, p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400488 do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400489 3, p->syms, p->strings, head);
Rich Felker368ba4a2011-06-25 00:18:19 -0400490 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400491 }
492}
493
Rich Felkere8dbf002011-06-25 00:47:28 -0400494static void free_all(struct dso *p)
495{
496 struct dso *n;
497 while (p) {
498 n = p->next;
499 if (p->map) free(p);
500 p = n;
501 }
502}
503
Rich Felkerc82f4a32012-01-23 00:57:38 -0500504static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
505{
506 for (; cnt--; ph = (void *)((char *)ph + stride))
507 if (ph->p_type == PT_DYNAMIC)
508 return ph->p_vaddr;
509 return 0;
510}
511
Rich Felker4ce3cb52012-02-06 14:39:09 -0500512static void do_init_fini(struct dso *p)
513{
514 size_t dyn[DYN_CNT] = {0};
515 for (; p; p=p->prev) {
516 if (p->constructed) return;
517 decode_vec(p->dynv, dyn, DYN_CNT);
518 if (dyn[0] & (1<<DT_FINI))
519 atexit((void (*)(void))(p->base + dyn[DT_FINI]));
520 if (dyn[0] & (1<<DT_INIT))
521 ((void (*)(void))(p->base + dyn[DT_INIT]))();
522 p->constructed = 1;
523 }
524}
525
Rich Felker3ec8d292012-04-25 00:05:42 -0400526void _dl_debug_state(void)
527{
528}
529
Rich Felkerc82f4a32012-01-23 00:57:38 -0500530void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400531{
532 size_t *auxv, aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400533 size_t i;
534 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400535 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400536 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400537 struct dso *const app = builtin_dsos+0;
538 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400539 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400540 char *env_preload=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400541
542 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400543 for (i=argc+1; argv[i]; i++)
544 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
545 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400546 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
547 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400548 auxv = (void *)(argv+i+1);
549
550 decode_vec(auxv, aux, AUX_CNT);
551
Rich Felker568b8072011-06-25 01:56:34 -0400552 /* Only trust user/env if kernel says we're not suid/sgid */
553 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400554 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400555 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400556 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400557 }
558
Rich Felker5c1909a2012-05-27 16:01:44 -0400559 /* If the dynamic linker was invoked as a program itself, AT_BASE
560 * will not be set. In that case, we assume the base address is
561 * the start of the page containing the PHDRs; I don't know any
562 * better approach... */
563 if (!aux[AT_BASE]) {
564 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
565 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
566 }
567
Rich Felkerc82f4a32012-01-23 00:57:38 -0500568 /* The dynamic linker load address is passed by the kernel
569 * in the AUX vector, so this is easy. */
570 lib->base = (void *)aux[AT_BASE];
Rich Felker5c1909a2012-05-27 16:01:44 -0400571 lib->name = lib->shortname = "libc.so";
Rich Felkerc82f4a32012-01-23 00:57:38 -0500572 lib->global = 1;
573 ehdr = (void *)lib->base;
574 lib->dynv = (void *)(lib->base + find_dyn(
575 (void *)(aux[AT_BASE]+ehdr->e_phoff),
576 ehdr->e_phnum, ehdr->e_phentsize));
577 decode_dyn(lib);
578
Rich Felker5c1909a2012-05-27 16:01:44 -0400579 if (aux[AT_PHDR]) {
Rich Felker649cec52012-07-13 01:31:02 -0400580 size_t interp_off = 0;
Rich Felker5c1909a2012-05-27 16:01:44 -0400581 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
582 phdr = (void *)aux[AT_PHDR];
583 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
584 if (phdr->p_type == PT_PHDR)
585 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
Rich Felker649cec52012-07-13 01:31:02 -0400586 else if (phdr->p_type == PT_INTERP)
587 interp_off = (size_t)phdr->p_vaddr;
Rich Felker5c1909a2012-05-27 16:01:44 -0400588 }
Rich Felker649cec52012-07-13 01:31:02 -0400589 if (interp_off) lib->name = (char *)app->base + interp_off;
Rich Felker0420b872012-07-11 01:41:20 -0400590 app->name = argv[0];
Rich Felker5c1909a2012-05-27 16:01:44 -0400591 app->dynv = (void *)(app->base + find_dyn(
592 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
593 } else {
594 int fd;
595 char *ldname = argv[0];
596 size_t dyno, l = strlen(ldname);
597 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
598 *argv++ = (void *)-1;
599 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
600 if (!argv[0]) {
601 dprintf(2, "musl libc/dynamic program loader\n");
602 dprintf(2, "usage: %s pathname%s\n", ldname,
603 ldd_mode ? "" : " [args]");
604 _exit(1);
605 }
606 fd = open(argv[0], O_RDONLY);
607 if (fd < 0) {
608 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
609 _exit(1);
610 }
611 runtime = 1;
612 ehdr = (void *)map_library(fd, &app->map_len, &app->base, &dyno);
613 if (!ehdr) {
614 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
615 _exit(1);
616 }
617 runtime = 0;
618 close(fd);
Rich Felker649cec52012-07-13 01:31:02 -0400619 lib->name = ldname;
Rich Felker0420b872012-07-11 01:41:20 -0400620 app->name = argv[0];
Rich Felker5c1909a2012-05-27 16:01:44 -0400621 app->dynv = (void *)(app->base + dyno);
622 aux[AT_ENTRY] = ehdr->e_entry;
Rich Felkere12fe652012-01-23 02:02:59 -0500623 }
Rich Felkerc82f4a32012-01-23 00:57:38 -0500624 app->global = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500625 app->constructed = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500626 decode_dyn(app);
627
628 /* Attach to vdso, if provided by the kernel */
Rich Felkercf8506a2011-08-16 08:50:03 -0400629 for (i=0; auxv[i]; i+=2) {
Rich Felkerc82f4a32012-01-23 00:57:38 -0500630 size_t vdso_base = auxv[i+1];
631 if (auxv[i] != AT_SYSINFO_EHDR) continue;
Rich Felker6ab444d2011-07-24 00:54:55 -0400632 ehdr = (void *)vdso_base;
633 phdr = (void *)(vdso_base + ehdr->e_phoff);
634 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
635 if (phdr->p_type == PT_DYNAMIC)
636 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
637 if (phdr->p_type == PT_LOAD)
638 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
639 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400640 vdso->name = vdso->shortname = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400641 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500642 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400643 vdso->prev = lib;
644 lib->next = vdso;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500645 break;
Rich Felker6ab444d2011-07-24 00:54:55 -0400646 }
647
Rich Felkerc82f4a32012-01-23 00:57:38 -0500648 /* Initial dso chain consists only of the app. We temporarily
649 * append the dynamic linker/libc so we can relocate it, then
650 * restore the initial chain in preparation for loading third
651 * party libraries (preload/needed). */
652 head = tail = app;
653 libc = lib;
654 app->next = lib;
655 reloc_all(lib);
656 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400657
Rich Felkerc82f4a32012-01-23 00:57:38 -0500658 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400659
Rich Felkerc82f4a32012-01-23 00:57:38 -0500660 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400661 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
662 ehdr = (void *)lib->base;
663 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400664 ehdr->e_phentsize, ehdr->e_phnum);
665
Rich Felkerc82f4a32012-01-23 00:57:38 -0500666 /* Load preload/needed libraries, add their symbols to the global
Rich Felkerfd7015d2012-01-23 18:32:40 -0500667 * namespace, and perform all remaining relocations. The main
668 * program must be relocated LAST since it may contain copy
669 * relocations which depend on libraries' relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -0400670 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500671 load_deps(app);
672 make_global(app);
Rich Felkerfd7015d2012-01-23 18:32:40 -0500673 reloc_all(app->next);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500674 reloc_all(app);
Rich Felker51e2d832011-06-18 19:48:42 -0400675
Rich Felker5c1909a2012-05-27 16:01:44 -0400676 if (ldd_mode) _exit(0);
677
Rich Felkerc82f4a32012-01-23 00:57:38 -0500678 /* Switch to runtime mode: any further failures in the dynamic
679 * linker are a reportable failure rather than a fatal startup
680 * error. If the dynamic loader (dlopen) will not be used, free
681 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -0400682 runtime = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500683
Rich Felker3ec8d292012-04-25 00:05:42 -0400684 for (i=0; app->dynv[i]; i+=2)
685 if (app->dynv[i]==DT_DEBUG)
686 app->dynv[i+1] = (size_t)&debug;
687 debug.ver = 1;
688 debug.bp = _dl_debug_state;
689 debug.head = head;
690 debug.base = lib->base;
691 debug.state = 0;
692 _dl_debug_state();
693
Rich Felker58aa5f42012-05-03 20:42:45 -0400694 if (ssp_used) __init_ssp(auxv);
695
Rich Felker4ce3cb52012-02-06 14:39:09 -0500696 do_init_fini(tail);
697
Rich Felkera53de812011-07-24 00:26:12 -0400698 if (!rtld_used) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400699 free_all(head);
700 free(sys_path);
Rich Felkera53de812011-07-24 00:26:12 -0400701 reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
Rich Felker59ab43f2011-06-26 19:23:28 -0400702 }
Rich Felkere8dbf002011-06-25 00:47:28 -0400703
Rich Felker51e2d832011-06-18 19:48:42 -0400704 errno = 0;
705 return (void *)aux[AT_ENTRY];
706}
Rich Felker59ab43f2011-06-26 19:23:28 -0400707
708void *dlopen(const char *file, int mode)
709{
Rich Felker2fdea172011-07-01 22:40:00 -0400710 struct dso *volatile p, *orig_tail = tail, *next;
Rich Felker59ab43f2011-06-26 19:23:28 -0400711 size_t i;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500712 int cs;
Rich Felker59ab43f2011-06-26 19:23:28 -0400713
714 if (!file) return head;
715
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500716 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker59ab43f2011-06-26 19:23:28 -0400717 pthread_rwlock_wrlock(&lock);
718
719 if (setjmp(rtld_fail)) {
720 /* Clean up anything new that was (partially) loaded */
Rich Felker92ab5d82011-06-26 21:21:04 -0400721 if (p->deps) for (i=0; p->deps[i]; i++)
722 if (p->deps[i]->global < 0)
723 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400724 for (p=orig_tail->next; p; p=next) {
725 next = p->next;
726 munmap(p->map, p->map_len);
727 free(p->deps);
728 free(p);
729 }
730 tail = orig_tail;
731 tail->next = 0;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500732 p = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400733 errflag = 1;
734 goto end;
Rich Felkera9e85c02012-03-23 00:28:20 -0400735 } else p = load_library(file);
736
737 if (!p) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400738 snprintf(errbuf, sizeof errbuf,
739 "Error loading shared library %s: %m", file);
Rich Felkera9e85c02012-03-23 00:28:20 -0400740 errflag = 1;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500741 goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -0400742 }
743
Rich Felker59ab43f2011-06-26 19:23:28 -0400744 /* First load handling */
745 if (!p->deps) {
746 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400747 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400748 if (!p->deps[i]->global)
749 p->deps[i]->global = -1;
750 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -0400751 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400752 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400753 if (p->deps[i]->global < 0)
754 p->deps[i]->global = 0;
755 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400756 }
757
758 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -0400759 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -0400760 p->deps[i]->global = 1;
761 p->global = 1;
762 }
763
Rich Felker3ec8d292012-04-25 00:05:42 -0400764 _dl_debug_state();
765
Rich Felkerce4d97e2012-02-06 17:57:29 -0500766 do_init_fini(tail);
Rich Felker06933cc2011-06-26 22:09:32 -0400767end:
Rich Felker59ab43f2011-06-26 19:23:28 -0400768 pthread_rwlock_unlock(&lock);
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500769 pthread_setcancelstate(cs, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -0400770 return p;
771}
772
Rich Felker623753a2011-08-16 00:42:13 -0400773static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400774{
775 size_t i;
776 uint32_t h;
777 Sym *sym;
Rich Felker623753a2011-08-16 00:42:13 -0400778 if (p == RTLD_NEXT) {
779 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
780 if (!p) p=head;
Rich Felkerd93e0282012-07-07 16:32:27 -0400781 void *res = find_sym(p->next, s, 0);
782 if (!res) goto failed;
783 return res;
Rich Felker623753a2011-08-16 00:42:13 -0400784 }
Rich Felkera9e85c02012-03-23 00:28:20 -0400785 if (p == head || p == RTLD_DEFAULT) {
786 void *res = find_sym(head, s, 0);
Rich Felker4027f4e2012-05-04 20:18:18 -0400787 if (!res) goto failed;
Rich Felkera9e85c02012-03-23 00:28:20 -0400788 return res;
789 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400790 h = hash(s);
791 sym = lookup(s, h, p->syms, p->hashtab, p->strings);
792 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
793 return p->base + sym->st_value;
794 if (p->deps) for (i=0; p->deps[i]; i++) {
795 sym = lookup(s, h, p->deps[i]->syms,
796 p->deps[i]->hashtab, p->deps[i]->strings);
797 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
798 return p->deps[i]->base + sym->st_value;
799 }
Rich Felker4027f4e2012-05-04 20:18:18 -0400800failed:
Rich Felkera9e85c02012-03-23 00:28:20 -0400801 errflag = 1;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400802 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
Rich Felker59ab43f2011-06-26 19:23:28 -0400803 return 0;
804}
805
Rich Felker623753a2011-08-16 00:42:13 -0400806void *__dlsym(void *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400807{
808 void *res;
809 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -0400810 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -0400811 pthread_rwlock_unlock(&lock);
812 return res;
813}
Rich Felker5a09a532012-02-03 03:16:07 -0500814#else
815void *dlopen(const char *file, int mode)
816{
817 return 0;
818}
819void *__dlsym(void *p, const char *s, void *ra)
820{
821 return 0;
822}
823#endif
Rich Felker59ab43f2011-06-26 19:23:28 -0400824
825char *dlerror()
826{
Rich Felkera9e85c02012-03-23 00:28:20 -0400827 if (!errflag) return 0;
828 errflag = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400829 return errbuf;
Rich Felker59ab43f2011-06-26 19:23:28 -0400830}
831
832int dlclose(void *p)
833{
834 return 0;
835}