libdl.c 15 KB

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