dlib.c 16 KB

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