libdl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * libdl.c
  3. *
  4. * Functions required for dlopen et. al.
  5. */
  6. #include <stdlib.h>
  7. #include <features.h>
  8. #include "dlfcn.h"
  9. #include "linuxelf.h"
  10. #include "ld_syscall.h"
  11. #include "ld_hash.h"
  12. #include "ld_string.h"
  13. extern int _dl_error_number;
  14. extern struct r_debug *_dl_debug_addr;
  15. extern void *(*_dl_malloc_function) (size_t size);
  16. static int do_fixup(struct elf_resolve *tpnt, int flag);
  17. static int do_dlclose(void *, int need_fini);
  18. void *dlopen(const char *, int) __attribute__ ((__weak__, __alias__ ("_dlopen")));
  19. const char *dlerror(void) __attribute__ ((__weak__, __alias__ ("_dlerror")));
  20. void *dlsym(void *, const char *) __attribute__ ((__weak__, __alias__ ("_dlsym")));
  21. int dlclose(void *) __attribute__ ((__weak__, __alias__ ("_dlclose")));
  22. int dladdr(void *, Dl_info *) __attribute__ ((__weak__, __alias__ ("_dladdr")));
  23. static const char *dl_error_names[] = {
  24. "",
  25. "File not found",
  26. "Unable to open /dev/zero",
  27. "Not an ELF file",
  28. #if defined (__i386__)
  29. "Not i386 binary",
  30. #elif defined (__sparc__)
  31. "Not sparc binary",
  32. #elif defined (__mc68000__)
  33. "Not m68k binary",
  34. #else
  35. "Unrecognized binary type",
  36. #endif
  37. "Not an ELF shared library",
  38. "Unable to mmap file",
  39. "No dynamic section",
  40. #ifdef ELF_USES_RELOCA
  41. "Unable to process REL relocs",
  42. #else
  43. "Unable to process RELA relocs",
  44. #endif
  45. "Bad handle",
  46. "Unable to resolve symbol"
  47. };
  48. static void dl_cleanup(void) __attribute__ ((destructor));
  49. static void dl_cleanup(void)
  50. {
  51. struct dyn_elf *d;
  52. for (d = _dl_handles; d; d = d->next_handle)
  53. if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
  54. (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
  55. d->dyn->dynamic_info[DT_FINI] = 0;
  56. }
  57. }
  58. void *_dlopen(const char *libname, int flag)
  59. {
  60. struct elf_resolve *tpnt, *tfrom;
  61. struct dyn_elf *rpnt = NULL;
  62. struct dyn_elf *dyn_chain;
  63. struct dyn_elf *dpnt;
  64. static int dl_init = 0;
  65. char *from;
  66. void (*dl_brk) (void);
  67. int (*dl_elf_init) (void);
  68. from = __builtin_return_address(0);
  69. /* Have the dynamic linker use the regular malloc function now */
  70. if (!dl_init) {
  71. dl_init++;
  72. _dl_malloc_function = malloc;
  73. }
  74. /* Cover the trivial case first */
  75. if (!libname)
  76. return _dl_symbol_tables;
  77. #ifdef USE_CACHE
  78. _dl_map_cache();
  79. #endif
  80. /*
  81. * Try and locate the module we were called from - we
  82. * need this so that we get the correct RPATH. Note that
  83. * this is the current behavior under Solaris, but the
  84. * ABI+ specifies that we should only use the RPATH from
  85. * the application. Thus this may go away at some time
  86. * in the future.
  87. */
  88. tfrom = NULL;
  89. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  90. tpnt = dpnt->dyn;
  91. if (tpnt->loadaddr < from
  92. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
  93. tfrom = tpnt;
  94. }
  95. if (!(tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname))) {
  96. #ifdef USE_CACHE
  97. _dl_unmap_cache();
  98. #endif
  99. return NULL;
  100. }
  101. dyn_chain = rpnt = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  102. _dl_memset(rpnt, 0, sizeof(*rpnt));
  103. rpnt->dyn = tpnt;
  104. rpnt->flags = flag;
  105. if (!tpnt->symbol_scope)
  106. tpnt->symbol_scope = dyn_chain;
  107. rpnt->next_handle = _dl_handles;
  108. _dl_handles = rpnt;
  109. /*
  110. * OK, we have the requested file in memory. Now check for
  111. * any other requested files that may also be required.
  112. */
  113. {
  114. struct elf_resolve *tcurr;
  115. struct elf_resolve * tpnt1;
  116. Elf32_Dyn * dpnt;
  117. char * lpnt;
  118. tcurr = tpnt;
  119. do{
  120. for(dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++)
  121. {
  122. if(dpnt->d_tag == DT_NEEDED)
  123. {
  124. lpnt = tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  125. dpnt->d_un.d_val;
  126. if(!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpnt)))
  127. goto oops;
  128. rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  129. _dl_memset (rpnt->next, 0, sizeof (*(rpnt->next)));
  130. rpnt = rpnt->next;
  131. if (!tpnt1->symbol_scope) tpnt1->symbol_scope = dyn_chain;
  132. rpnt->dyn = tpnt1;
  133. };
  134. }
  135. tcurr = tcurr->next;
  136. } while(tcurr);
  137. }
  138. /*
  139. * OK, now attach the entire chain at the end
  140. */
  141. rpnt->next = _dl_symbol_tables;
  142. if (do_fixup(tpnt, flag)) {
  143. _dl_error_number = DL_NO_SYMBOL;
  144. goto oops;
  145. }
  146. dl_brk = (void (*)()) _dl_debug_addr->r_brk;
  147. if (dl_brk != NULL) {
  148. _dl_debug_addr->r_state = RT_ADD;
  149. (*dl_brk) ();
  150. _dl_debug_addr->r_state = RT_CONSISTENT;
  151. (*dl_brk) ();
  152. }
  153. for (rpnt = dyn_chain; rpnt; rpnt = rpnt->next) {
  154. tpnt = rpnt->dyn;
  155. /* Apparently crt1 for the application is responsible for handling this.
  156. * We only need to run the init/fini for shared libraries
  157. */
  158. if (tpnt->libtype == elf_executable)
  159. continue;
  160. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  161. continue;
  162. tpnt->init_flag |= INIT_FUNCS_CALLED;
  163. if (tpnt->dynamic_info[DT_INIT]) {
  164. dl_elf_init = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  165. (*dl_elf_init) ();
  166. }
  167. }
  168. #ifdef USE_CACHE
  169. _dl_unmap_cache();
  170. #endif
  171. return (void *) dyn_chain;
  172. oops:
  173. /* Something went wrong. Clean up and return NULL. */
  174. #ifdef USE_CACHE
  175. _dl_unmap_cache();
  176. #endif
  177. do_dlclose(dyn_chain, 0);
  178. return NULL;
  179. }
  180. static int do_fixup(struct elf_resolve *tpnt, int flag)
  181. {
  182. int goof = 0;
  183. if (tpnt->next)
  184. goof += do_fixup(tpnt->next, flag);
  185. if (tpnt->dynamic_info[DT_REL]) {
  186. #ifdef ELF_USES_RELOCA
  187. goof++;
  188. #else
  189. if (tpnt->init_flag & RELOCS_DONE)
  190. return goof;
  191. tpnt->init_flag |= RELOCS_DONE;
  192. goof += _dl_parse_relocation_information(tpnt,
  193. tpnt->dynamic_info[DT_REL], tpnt->dynamic_info[DT_RELSZ], 0);
  194. #endif
  195. }
  196. if (tpnt->dynamic_info[DT_RELA]) {
  197. #ifdef ELF_USES_RELOCA
  198. if (tpnt->init_flag & RELOCS_DONE)
  199. return goof;
  200. tpnt->init_flag |= RELOCS_DONE;
  201. goof += _dl_parse_relocation_information(tpnt,
  202. tpnt->dynamic_info[DT_RELA], tpnt->dynamic_info[DT_RELASZ], 0);
  203. #else
  204. goof++;
  205. #endif
  206. }
  207. if (tpnt->dynamic_info[DT_JMPREL]) {
  208. if (tpnt->init_flag & JMP_RELOCS_DONE)
  209. return goof;
  210. tpnt->init_flag |= JMP_RELOCS_DONE;
  211. if (flag == RTLD_LAZY)
  212. _dl_parse_lazy_relocation_information(tpnt,
  213. tpnt->dynamic_info[DT_JMPREL],
  214. tpnt->dynamic_info[DT_PLTRELSZ], 0);
  215. else
  216. goof += _dl_parse_relocation_information(tpnt,
  217. tpnt->dynamic_info[DT_JMPREL],
  218. tpnt->dynamic_info[DT_PLTRELSZ], 0);
  219. };
  220. return goof;
  221. }
  222. void *_dlsym(void *vhandle, const char *name)
  223. {
  224. struct elf_resolve *tpnt, *tfrom;
  225. struct dyn_elf *handle;
  226. char *from;
  227. struct dyn_elf *rpnt;
  228. void *ret;
  229. handle = (struct dyn_elf *) vhandle;
  230. /* First of all verify that we have a real handle
  231. of some kind. Return NULL if not a valid handle. */
  232. if (handle == NULL)
  233. handle = _dl_symbol_tables;
  234. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  235. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  236. if (rpnt == handle)
  237. break;
  238. if (!rpnt) {
  239. _dl_error_number = DL_BAD_HANDLE;
  240. return NULL;
  241. }
  242. } else if (handle == RTLD_NEXT) {
  243. /*
  244. * Try and locate the module we were called from - we
  245. * need this so that we know where to start searching
  246. * from. We never pass RTLD_NEXT down into the actual
  247. * dynamic loader itself, as it doesn't know
  248. * how to properly treat it.
  249. */
  250. from = __builtin_return_address(0);
  251. tfrom = NULL;
  252. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  253. tpnt = rpnt->dyn;
  254. if (tpnt->loadaddr < from
  255. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  256. tfrom = tpnt;
  257. handle = rpnt->next;
  258. }
  259. }
  260. }
  261. ret = _dl_find_hash((char*)name, handle, NULL, 1);
  262. /*
  263. * Nothing found.
  264. */
  265. if (!ret)
  266. _dl_error_number = DL_NO_SYMBOL;
  267. return ret;
  268. }
  269. int _dlclose(void *vhandle)
  270. {
  271. return do_dlclose(vhandle, 1);
  272. }
  273. static int do_dlclose(void *vhandle, int need_fini)
  274. {
  275. struct dyn_elf *rpnt, *rpnt1;
  276. struct dyn_elf *spnt, *spnt1;
  277. elf_phdr *ppnt;
  278. struct elf_resolve *tpnt;
  279. int (*dl_elf_fini) (void);
  280. void (*dl_brk) (void);
  281. struct dyn_elf *handle;
  282. unsigned int end;
  283. int i = 0;
  284. handle = (struct dyn_elf *) vhandle;
  285. rpnt1 = NULL;
  286. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  287. if (rpnt == handle) {
  288. break;
  289. }
  290. rpnt1 = rpnt;
  291. }
  292. if (!rpnt) {
  293. _dl_error_number = DL_BAD_HANDLE;
  294. return 1;
  295. }
  296. /* OK, this is a valid handle - now close out the file.
  297. * We check if we need to call fini () on the handle. */
  298. spnt = need_fini ? handle : handle->next;
  299. for (; spnt; spnt = spnt1) {
  300. spnt1 = spnt->next;
  301. /* We appended the module list to the end - when we get back here,
  302. quit. The access counts were not adjusted to account for being here. */
  303. if (spnt == _dl_symbol_tables)
  304. break;
  305. if (spnt->dyn->usage_count == 1
  306. && spnt->dyn->libtype == loaded_file) {
  307. tpnt = spnt->dyn;
  308. /* Apparently crt1 for the application is responsible for handling this.
  309. * We only need to run the init/fini for shared libraries
  310. */
  311. if (tpnt->dynamic_info[DT_FINI]) {
  312. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
  313. tpnt->dynamic_info[DT_FINI]);
  314. (*dl_elf_fini) ();
  315. }
  316. }
  317. }
  318. if (rpnt1)
  319. rpnt1->next_handle = rpnt->next_handle;
  320. else
  321. _dl_handles = rpnt->next_handle;
  322. /* OK, this is a valid handle - now close out the file */
  323. for (rpnt = handle; rpnt; rpnt = rpnt1) {
  324. rpnt1 = rpnt->next;
  325. /* We appended the module list to the end - when we get back here,
  326. quit. The access counts were not adjusted to account for being here. */
  327. if (rpnt == _dl_symbol_tables)
  328. break;
  329. rpnt->dyn->usage_count--;
  330. if (rpnt->dyn->usage_count == 0
  331. && rpnt->dyn->libtype == loaded_file) {
  332. tpnt = rpnt->dyn;
  333. /* Apparently crt1 for the application is responsible for handling this.
  334. * We only need to run the init/fini for shared libraries
  335. */
  336. #if 0
  337. /* We have to do this above, before we start closing objects.
  338. * Otherwise when the needed symbols for _fini handling are
  339. * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
  340. if (tpnt->dynamic_info[DT_FINI]) {
  341. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  342. (*dl_elf_fini) ();
  343. }
  344. #endif
  345. end = 0;
  346. for (i = 0, ppnt = rpnt->dyn->ppnt;
  347. i < rpnt->dyn->n_phent; ppnt++, i++) {
  348. if (ppnt->p_type != PT_LOAD)
  349. continue;
  350. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  351. end = ppnt->p_vaddr + ppnt->p_memsz;
  352. }
  353. _dl_munmap(rpnt->dyn->loadaddr, end);
  354. /* Next, remove rpnt->dyn from the loaded_module list */
  355. if (_dl_loaded_modules == rpnt->dyn) {
  356. _dl_loaded_modules = rpnt->dyn->next;
  357. if (_dl_loaded_modules)
  358. _dl_loaded_modules->prev = 0;
  359. } else
  360. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  361. if (tpnt->next == rpnt->dyn) {
  362. tpnt->next = tpnt->next->next;
  363. if (tpnt->next)
  364. tpnt->next->prev = tpnt;
  365. break;
  366. }
  367. free(rpnt->dyn->libname);
  368. free(rpnt->dyn);
  369. }
  370. free(rpnt);
  371. }
  372. dl_brk = (void (*)()) _dl_debug_addr->r_brk;
  373. if (dl_brk != NULL) {
  374. _dl_debug_addr->r_state = RT_DELETE;
  375. (*dl_brk) ();
  376. _dl_debug_addr->r_state = RT_CONSISTENT;
  377. (*dl_brk) ();
  378. }
  379. return 0;
  380. }
  381. const char *_dlerror()
  382. {
  383. const char *retval;
  384. if (!_dl_error_number)
  385. return NULL;
  386. retval = dl_error_names[_dl_error_number];
  387. _dl_error_number = 0;
  388. return retval;
  389. }
  390. /* This is a real hack. We need access to the dynamic linker, but we
  391. also need to make it possible to link against this library without any
  392. unresolved externals. We provide these weak symbols to make the link
  393. possible, but at run time the normal symbols are accessed. */
  394. static void __attribute__ ((unused)) foobar()
  395. {
  396. _dl_dprintf(2, "libdl library not correctly linked\n");
  397. _dl_exit(1);
  398. }
  399. static int __attribute__ ((unused)) foobar1 = (int) foobar; /* Use as pointer */
  400. void _dl_dprintf(int, const char *, ...) __attribute__ ((__weak__, __alias__ ("foobar")));
  401. char *_dl_find_hash(char *, struct dyn_elf *, struct elf_resolve *, int)
  402. __attribute__ ((__weak__, __alias__ ("foobar")));
  403. struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **, struct elf_resolve *, char *)
  404. __attribute__ ((__weak__, __alias__ ("foobar")));
  405. int _dl_parse_relocation_information(struct elf_resolve *, unsigned long, unsigned long, int)
  406. __attribute__ ((__weak__, __alias__ ("foobar")));
  407. void _dl_parse_lazy_relocation_information(struct elf_resolve *, unsigned long, unsigned long, int)
  408. __attribute__ ((__weak__, __alias__ ("foobar")));
  409. #ifdef USE_CACHE
  410. int _dl_map_cache(void) __attribute__ ((__weak__, __alias__ ("foobar")));
  411. int _dl_unmap_cache(void) __attribute__ ((__weak__, __alias__ ("foobar")));
  412. #endif
  413. extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__, __alias__ ("foobar1")));
  414. extern struct dyn_elf *_dl_handles __attribute__ ((__weak__, __alias__ ("foobar1")));
  415. extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__, __alias__ ("foobar1")));
  416. extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__, __alias__ ("foobar1")));
  417. extern int _dl_error_number __attribute__ ((__weak__, __alias__ ("foobar1")));
  418. extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__, __alias__ ("foobar1")));
  419. /*
  420. * Dump information to stderrr about the current loaded modules
  421. */
  422. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  423. void _dlinfo()
  424. {
  425. struct elf_resolve *tpnt;
  426. struct dyn_elf *rpnt, *hpnt;
  427. _dl_dprintf(2, "List of loaded modules\n");
  428. /* First start with a complete list of all of the loaded files. */
  429. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  430. _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
  431. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  432. (unsigned) tpnt->symbol_scope,
  433. type[tpnt->libtype],
  434. tpnt->usage_count, tpnt->libname);
  435. }
  436. /* Next dump the module list for the application itself */
  437. _dl_dprintf(2, "\nModules for application (%x):\n",
  438. (unsigned) _dl_symbol_tables);
  439. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  440. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  441. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  442. _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
  443. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  444. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
  445. rpnt->dyn->libname);
  446. }
  447. }
  448. int _dladdr(void *__address, Dl_info * __dlip)
  449. {
  450. struct elf_resolve *pelf;
  451. struct elf_resolve *rpnt;
  452. #ifdef USE_CACHE
  453. _dl_map_cache();
  454. #endif
  455. /*
  456. * Try and locate the module address is in
  457. */
  458. pelf = NULL;
  459. #if 0
  460. _dl_dprintf(2, "dladdr( 0x%p, 0x%p )\n", __address, __dlip);
  461. #endif
  462. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  463. struct elf_resolve *tpnt;
  464. tpnt = rpnt;
  465. #if 0
  466. _dl_dprintf(2, "Module \"%s\" at 0x%p\n",
  467. tpnt->libname, tpnt->loadaddr);
  468. #endif
  469. if (tpnt->loadaddr < (char *) __address
  470. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  471. pelf = tpnt;
  472. }
  473. }
  474. if (!pelf) {
  475. return 0;
  476. }
  477. /*
  478. * Try and locate the symbol of address
  479. */
  480. {
  481. char *strtab;
  482. Elf32_Sym *symtab;
  483. int hn, si;
  484. int sf;
  485. int sn = 0;
  486. void *sa = 0;
  487. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  488. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  489. sf = 0;
  490. for (hn = 0; hn < pelf->nbucket; hn++) {
  491. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  492. void *symbol_addr;
  493. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  494. if (symbol_addr <= __address && (!sf || sa < symbol_addr)) {
  495. sa = symbol_addr;
  496. sn = si;
  497. sf = 1;
  498. }
  499. #if 0
  500. _dl_dprintf(2, "Symbol \"%s\" at 0x%p\n",
  501. strtab + symtab[si].st_name, symbol_addr);
  502. #endif
  503. }
  504. }
  505. if (sf) {
  506. __dlip->dli_fname = pelf->libname;
  507. __dlip->dli_fbase = pelf->loadaddr;
  508. __dlip->dli_sname = strtab + symtab[sn].st_name;
  509. __dlip->dli_saddr = sa;
  510. }
  511. return 1;
  512. }
  513. }