dynlink.c 30 KB

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