dlib.c 15 KB

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