dlib.c 14 KB

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