libdl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * libdl.c
  3. *
  4. * Functions required for dlopen et. al.
  5. */
  6. #include <ldso.h>
  7. /* The public interfaces */
  8. void *dlopen(const char *, int) __attribute__ ((__weak__, __alias__ ("_dlopen")));
  9. int dlclose(void *) __attribute__ ((__weak__, __alias__ ("_dlclose")));
  10. void *dlsym(void *, const char *) __attribute__ ((__weak__, __alias__ ("_dlsym")));
  11. const char *dlerror(void) __attribute__ ((__weak__, __alias__ ("_dlerror")));
  12. int dladdr(void *, Dl_info *) __attribute__ ((__weak__, __alias__ ("_dladdr")));
  13. void _dlinfo(void);
  14. #ifdef __PIC__
  15. /* This is a real hack. We need access to the dynamic linker, but we
  16. also need to make it possible to link against this library without any
  17. unresolved externals. We provide these weak symbols to make the link
  18. possible, but at run time the normal symbols are accessed. */
  19. static void __attribute__ ((unused)) foobar(void)
  20. {
  21. const char msg[]="libdl library not correctly linked\n";
  22. _dl_write(2, msg, _dl_strlen(msg));
  23. _dl_exit(1);
  24. }
  25. static int __attribute__ ((unused)) foobar1 = (int) foobar; /* Use as pointer */
  26. extern void _dl_dprintf(int, const char *, ...) __attribute__ ((__weak__, __alias__ ("foobar")));
  27. extern char *_dl_find_hash(const char *, struct dyn_elf *, struct elf_resolve *, enum caller_type)
  28. __attribute__ ((__weak__, __alias__ ("foobar")));
  29. extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **, struct elf_resolve *, char *)
  30. __attribute__ ((__weak__, __alias__ ("foobar")));
  31. extern int _dl_fixup(struct elf_resolve *tpnt, int lazy)
  32. __attribute__ ((__weak__, __alias__ ("foobar")));
  33. extern int _dl_copy_fixups(struct dyn_elf * tpnt)
  34. __attribute__ ((__weak__, __alias__ ("foobar")));
  35. #ifdef __mips__
  36. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt)
  37. __attribute__ ((__weak__, __alias__ ("foobar")));
  38. #endif
  39. #ifdef USE_CACHE
  40. int _dl_map_cache(void) __attribute__ ((__weak__, __alias__ ("foobar")));
  41. int _dl_unmap_cache(void) __attribute__ ((__weak__, __alias__ ("foobar")));
  42. #endif
  43. extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__, __alias__ ("foobar1")));
  44. extern struct dyn_elf *_dl_handles __attribute__ ((__weak__, __alias__ ("foobar1")));
  45. extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__, __alias__ ("foobar1")));
  46. extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__, __alias__ ("foobar1")));
  47. extern unsigned long _dl_error_number __attribute__ ((__weak__, __alias__ ("foobar1")));
  48. extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__, __alias__ ("foobar1")));
  49. #ifdef __SUPPORT_LD_DEBUG__
  50. #define _dl_debug_file 2
  51. #endif
  52. #else
  53. #ifdef __SUPPORT_LD_DEBUG__
  54. static char *_dl_debug = 0;
  55. static char *_dl_debug_symbols = 0;
  56. static char *_dl_debug_move = 0;
  57. static char *_dl_debug_reloc = 0;
  58. static char *_dl_debug_detail = 0;
  59. static char *_dl_debug_nofixups = 0;
  60. static char *_dl_debug_bindings = 0;
  61. static int _dl_debug_file = 2;
  62. #elif defined __SUPPORT_LD_DEBUG_EARLY__
  63. #define _dl_debug_file 2
  64. #endif
  65. char *_dl_library_path = 0;
  66. char *_dl_ldsopath = 0;
  67. struct r_debug *_dl_debug_addr = NULL;
  68. static char *_dl_malloc_addr, *_dl_mmap_zero;
  69. #include "../ldso/_dl_progname.h" /* Pull in the name of ld.so */
  70. #include "../ldso/hash.c"
  71. #include "../ldso/readelflib1.c"
  72. void *(*_dl_malloc_function) (size_t size);
  73. int _dl_fixup(struct elf_resolve *tpnt, int lazy);
  74. #endif
  75. static int do_dlclose(void *, int need_fini);
  76. static const char *dl_error_names[] = {
  77. "",
  78. "File not found",
  79. "Unable to open /dev/zero",
  80. "Not an ELF file",
  81. #if defined (__i386__)
  82. "Not i386 binary",
  83. #elif defined (__sparc__)
  84. "Not sparc binary",
  85. #elif defined (__mc68000__)
  86. "Not m68k binary",
  87. #else
  88. "Unrecognized binary type",
  89. #endif
  90. "Not an ELF shared library",
  91. "Unable to mmap file",
  92. "No dynamic section",
  93. #ifdef ELF_USES_RELOCA
  94. "Unable to process REL relocs",
  95. #else
  96. "Unable to process RELA relocs",
  97. #endif
  98. "Bad handle",
  99. "Unable to resolve symbol"
  100. };
  101. static void __attribute__ ((destructor)) dl_cleanup(void)
  102. {
  103. struct dyn_elf *d;
  104. for (d = _dl_handles; d; d = d->next_handle)
  105. if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
  106. (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
  107. d->dyn->dynamic_info[DT_FINI] = 0;
  108. }
  109. }
  110. void *_dlopen(const char *libname, int flag)
  111. {
  112. struct elf_resolve *tpnt, *tfrom, *tcurr;
  113. struct dyn_elf *dyn_chain, *rpnt = NULL;
  114. struct dyn_elf *dpnt;
  115. static int dl_init = 0;
  116. ElfW(Addr) from;
  117. const char *libname1, *libname2, *ptr;
  118. struct elf_resolve *tpnt1;
  119. void (*dl_brk) (void);
  120. /* A bit of sanity checking... */
  121. if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
  122. _dl_error_number = LD_BAD_HANDLE;
  123. return NULL;
  124. }
  125. from = (ElfW(Addr)) __builtin_return_address(0);
  126. /* Have the dynamic linker use the regular malloc function now */
  127. if (!dl_init) {
  128. dl_init++;
  129. _dl_malloc_function = malloc;
  130. }
  131. /* Cover the trivial case first */
  132. if (!libname)
  133. return _dl_symbol_tables;
  134. _dl_map_cache();
  135. /*
  136. * Try and locate the module we were called from - we
  137. * need this so that we get the correct RPATH. Note that
  138. * this is the current behavior under Solaris, but the
  139. * ABI+ specifies that we should only use the RPATH from
  140. * the application. Thus this may go away at some time
  141. * in the future.
  142. */
  143. tfrom = NULL;
  144. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  145. tpnt = dpnt->dyn;
  146. if (tpnt->loadaddr < from
  147. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
  148. tfrom = tpnt;
  149. }
  150. /* Skip over any initial initial './' and '/' stuff to
  151. * get the short form libname with no path garbage */
  152. libname1 = libname;
  153. ptr = _dl_strrchr(libname1, '/');
  154. if (ptr) {
  155. libname1 = ptr + 1;
  156. }
  157. /* Weed out duplicates early to avoid function aliasing */
  158. for (tpnt1 = _dl_loaded_modules; tpnt1; tpnt1 = tpnt1->next) {
  159. /* Skip over any initial initial './' and '/' stuff to
  160. * get the short form libname with no path garbage */
  161. libname2 = tpnt1->libname;
  162. ptr = _dl_strrchr(libname2, '/');
  163. if (ptr) {
  164. libname2 = ptr + 1;
  165. }
  166. if (_dl_strcmp(libname1, libname2) == 0) {
  167. /* Well, that was certainly easy */
  168. return tpnt1;
  169. }
  170. }
  171. /* Try to load the specified library */
  172. #ifdef __SUPPORT_LD_DEBUG__
  173. _dl_dprintf(_dl_debug_file, "Trying to dlopen '%s'\n", (char*)libname);
  174. #endif
  175. if (!(tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname))) {
  176. _dl_unmap_cache();
  177. return NULL;
  178. }
  179. //tpnt->libtype = loaded_file;
  180. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  181. _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
  182. dyn_chain->dyn = tpnt;
  183. dyn_chain->flags = flag;
  184. if (!tpnt->symbol_scope)
  185. tpnt->symbol_scope = dyn_chain;
  186. dyn_chain->next_handle = _dl_handles;
  187. _dl_handles = rpnt = dyn_chain;
  188. #ifdef __SUPPORT_LD_DEBUG__
  189. _dl_dprintf(_dl_debug_file, "Looking for needed libraries\n");
  190. #endif
  191. for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
  192. {
  193. Elf32_Dyn *dpnt;
  194. char *lpntstr;
  195. for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
  196. if (dpnt->d_tag == DT_NEEDED) {
  197. lpntstr = (char*)tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  198. dpnt->d_un.d_val;
  199. /* Skip over any initial initial './' and '/' stuff to
  200. * get the short form libname with no path garbage */
  201. libname1 = lpntstr;
  202. ptr = _dl_strrchr(libname1, '/');
  203. if (ptr) {
  204. libname1 = ptr + 1;
  205. }
  206. /* Linked with glibc? */
  207. if (_dl_strcmp(libname1, "libc.so.6") == 0) {
  208. _dl_dprintf(2, "\tERROR: %s is linked with GNU libc!\n",
  209. tcurr->libname);
  210. goto oops;
  211. }
  212. #if 0
  213. {
  214. struct elf_resolve *tpnt2;
  215. /* Weed out duplicates early to avoid function aliasing */
  216. for (tpnt2 = _dl_loaded_modules; tpnt2; tpnt2 = tpnt2->next) {
  217. /* Skip over any initial initial './' and '/' stuff to
  218. * get the short form libname with no path garbage */
  219. libname2 = tpnt2->libname;
  220. ptr = _dl_strrchr(libname2, '/');
  221. if (ptr) {
  222. libname2 = ptr + 1;
  223. }
  224. if (_dl_strcmp(libname1, libname2) == 0) {
  225. /* Well, that was certainly easy */
  226. #ifdef __SUPPORT_LD_DEBUG__
  227. _dl_dprintf(_dl_debug_file, "\tLibrary '%s' needed by '%s' "
  228. "already loaded\n", lpntstr, tcurr->libname);
  229. #endif
  230. continue;
  231. }
  232. }
  233. }
  234. #endif
  235. #ifdef __SUPPORT_LD_DEBUG__
  236. _dl_dprintf(_dl_debug_file, "Trying to load '%s', needed by '%s'\n",
  237. lpntstr, tcurr->libname);
  238. #endif
  239. #if 1
  240. if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr))) {
  241. goto oops;
  242. }
  243. #else
  244. if (!(tpnt1 = _dlopen(lpntstr, flag))) {
  245. goto oops;
  246. }
  247. #endif
  248. rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  249. _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
  250. rpnt = rpnt->next;
  251. if (!tpnt1->symbol_scope) tpnt1->symbol_scope = rpnt;
  252. rpnt->dyn = tpnt1;
  253. }
  254. }
  255. }
  256. /*
  257. * OK, now attach the entire chain at the end
  258. */
  259. rpnt->next = _dl_symbol_tables;
  260. #ifdef __mips__
  261. /*
  262. * Relocation of the GOT entries for MIPS have to be done
  263. * after all the libraries have been loaded.
  264. */
  265. _dl_perform_mips_global_got_relocations(tpnt);
  266. #endif
  267. #ifdef __SUPPORT_LD_DEBUG__
  268. _dl_dprintf(_dl_debug_file, "Beginning dlopen relocation fixups\n");
  269. #endif
  270. /*
  271. * OK, now all of the kids are tucked into bed in their proper addresses.
  272. * Now we go through and look for REL and RELA records that indicate fixups
  273. * to the GOT tables. We need to do this in reverse order so that COPY
  274. * directives work correctly */
  275. if (_dl_fixup(dyn_chain->dyn, (flag & RTLD_LAZY)))
  276. goto oops;
  277. #ifdef __SUPPORT_LD_DEBUG__
  278. _dl_dprintf(_dl_debug_file, "Beginning dlopen copy fixups\n");
  279. #endif
  280. if (_dl_symbol_tables) {
  281. if (_dl_copy_fixups(dyn_chain))
  282. goto oops;
  283. }
  284. /* TODO: Should we set the protections of all pages back to R/O now ? */
  285. /* Notify the debugger we have added some objects. */
  286. _dl_debug_addr->r_state = RT_ADD;
  287. if (_dl_debug_addr) {
  288. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  289. if (dl_brk != NULL) {
  290. _dl_debug_addr->r_state = RT_ADD;
  291. (*dl_brk) ();
  292. _dl_debug_addr->r_state = RT_CONSISTENT;
  293. (*dl_brk) ();
  294. }
  295. }
  296. #if 1
  297. #ifdef __SUPPORT_LD_DEBUG__
  298. _dlinfo();
  299. #endif
  300. #endif
  301. #ifdef __PIC__
  302. /* Find the last library so we can run things in the right order */
  303. for (rpnt = _dl_symbol_tables; rpnt!=NULL&& rpnt->next!=NULL; rpnt=rpnt->next)
  304. ;
  305. /* Run the ctors and set up the dtors */
  306. for (;rpnt!=NULL; rpnt=rpnt->prev)
  307. {
  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. tpnt = rpnt->dyn;
  312. if (tpnt->libtype == program_interpreter)
  313. continue;
  314. if (tpnt->libtype == elf_executable)
  315. continue;
  316. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  317. continue;
  318. tpnt->init_flag |= INIT_FUNCS_CALLED;
  319. if (tpnt->dynamic_info[DT_INIT]) {
  320. void (*dl_elf_func) (void);
  321. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  322. if (dl_elf_func && *dl_elf_func != NULL) {
  323. #ifdef __SUPPORT_LD_DEBUG__
  324. _dl_dprintf(2, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
  325. #endif
  326. (*dl_elf_func) ();
  327. }
  328. }
  329. if (tpnt->dynamic_info[DT_FINI]) {
  330. void (*dl_elf_func) (void);
  331. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  332. if (dl_elf_func && *dl_elf_func != NULL) {
  333. #ifdef __SUPPORT_LD_DEBUG__
  334. _dl_dprintf(2, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
  335. #endif
  336. atexit(dl_elf_func);
  337. }
  338. }
  339. }
  340. #endif
  341. return (void *) dyn_chain;
  342. oops:
  343. /* Something went wrong. Clean up and return NULL. */
  344. _dl_unmap_cache();
  345. do_dlclose(dyn_chain, 0);
  346. return NULL;
  347. }
  348. void *_dlsym(void *vhandle, const char *name)
  349. {
  350. struct elf_resolve *tpnt, *tfrom;
  351. struct dyn_elf *handle;
  352. ElfW(Addr) from;
  353. struct dyn_elf *rpnt;
  354. void *ret;
  355. handle = (struct dyn_elf *) vhandle;
  356. /* First of all verify that we have a real handle
  357. of some kind. Return NULL if not a valid handle. */
  358. if (handle == NULL)
  359. handle = _dl_symbol_tables;
  360. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  361. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  362. if (rpnt == handle)
  363. break;
  364. if (!rpnt) {
  365. _dl_error_number = LD_BAD_HANDLE;
  366. return NULL;
  367. }
  368. } else if (handle == RTLD_NEXT) {
  369. /*
  370. * Try and locate the module we were called from - we
  371. * need this so that we know where to start searching
  372. * from. We never pass RTLD_NEXT down into the actual
  373. * dynamic loader itself, as it doesn't know
  374. * how to properly treat it.
  375. */
  376. from = (ElfW(Addr)) __builtin_return_address(0);
  377. tfrom = NULL;
  378. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  379. tpnt = rpnt->dyn;
  380. if (tpnt->loadaddr < from
  381. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  382. tfrom = tpnt;
  383. handle = rpnt->next;
  384. }
  385. }
  386. }
  387. ret = _dl_find_hash((char*)name, handle, NULL, copyrel);
  388. /*
  389. * Nothing found.
  390. */
  391. if (!ret)
  392. _dl_error_number = LD_NO_SYMBOL;
  393. return ret;
  394. }
  395. int _dlclose(void *vhandle)
  396. {
  397. return do_dlclose(vhandle, 1);
  398. }
  399. static int do_dlclose(void *vhandle, int need_fini)
  400. {
  401. struct dyn_elf *rpnt, *rpnt1;
  402. struct dyn_elf *spnt, *spnt1;
  403. ElfW(Phdr) *ppnt;
  404. struct elf_resolve *tpnt;
  405. int (*dl_elf_fini) (void);
  406. void (*dl_brk) (void);
  407. struct dyn_elf *handle;
  408. unsigned int end;
  409. int i = 0;
  410. handle = (struct dyn_elf *) vhandle;
  411. rpnt1 = NULL;
  412. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  413. if (rpnt == handle) {
  414. break;
  415. }
  416. rpnt1 = rpnt;
  417. }
  418. if (!rpnt) {
  419. _dl_error_number = LD_BAD_HANDLE;
  420. return 1;
  421. }
  422. /* OK, this is a valid handle - now close out the file.
  423. * We check if we need to call fini () on the handle. */
  424. spnt = need_fini ? handle : handle->next;
  425. for (; spnt; spnt = spnt1) {
  426. spnt1 = spnt->next;
  427. /* We appended the module list to the end - when we get back here,
  428. quit. The access counts were not adjusted to account for being here. */
  429. if (spnt == _dl_symbol_tables)
  430. break;
  431. if (spnt->dyn->usage_count == 1
  432. && spnt->dyn->libtype == loaded_file) {
  433. tpnt = spnt->dyn;
  434. /* Apparently crt1 for the application is responsible for handling this.
  435. * We only need to run the init/fini for shared libraries
  436. */
  437. if (tpnt->dynamic_info[DT_FINI]) {
  438. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
  439. tpnt->dynamic_info[DT_FINI]);
  440. (*dl_elf_fini) ();
  441. }
  442. }
  443. }
  444. if (rpnt1)
  445. rpnt1->next_handle = rpnt->next_handle;
  446. else
  447. _dl_handles = rpnt->next_handle;
  448. /* OK, this is a valid handle - now close out the file */
  449. for (rpnt = handle; rpnt; rpnt = rpnt1) {
  450. rpnt1 = rpnt->next;
  451. /* We appended the module list to the end - when we get back here,
  452. quit. The access counts were not adjusted to account for being here. */
  453. if (rpnt == _dl_symbol_tables)
  454. break;
  455. rpnt->dyn->usage_count--;
  456. if (rpnt->dyn->usage_count == 0
  457. && rpnt->dyn->libtype == loaded_file) {
  458. tpnt = rpnt->dyn;
  459. /* Apparently crt1 for the application is responsible for handling this.
  460. * We only need to run the init/fini for shared libraries
  461. */
  462. #if 0
  463. /* We have to do this above, before we start closing objects.
  464. * Otherwise when the needed symbols for _fini handling are
  465. * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
  466. if (tpnt->dynamic_info[DT_FINI]) {
  467. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  468. (*dl_elf_fini) ();
  469. }
  470. #endif
  471. end = 0;
  472. for (i = 0, ppnt = rpnt->dyn->ppnt;
  473. i < rpnt->dyn->n_phent; ppnt++, i++) {
  474. if (ppnt->p_type != PT_LOAD)
  475. continue;
  476. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  477. end = ppnt->p_vaddr + ppnt->p_memsz;
  478. }
  479. _dl_munmap((void*)rpnt->dyn->loadaddr, end);
  480. /* Next, remove rpnt->dyn from the loaded_module list */
  481. if (_dl_loaded_modules == rpnt->dyn) {
  482. _dl_loaded_modules = rpnt->dyn->next;
  483. if (_dl_loaded_modules)
  484. _dl_loaded_modules->prev = 0;
  485. } else
  486. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  487. if (tpnt->next == rpnt->dyn) {
  488. tpnt->next = tpnt->next->next;
  489. if (tpnt->next)
  490. tpnt->next->prev = tpnt;
  491. break;
  492. }
  493. free(rpnt->dyn->libname);
  494. free(rpnt->dyn);
  495. }
  496. free(rpnt);
  497. }
  498. if (_dl_debug_addr) {
  499. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  500. if (dl_brk != NULL) {
  501. _dl_debug_addr->r_state = RT_DELETE;
  502. (*dl_brk) ();
  503. _dl_debug_addr->r_state = RT_CONSISTENT;
  504. (*dl_brk) ();
  505. }
  506. }
  507. return 0;
  508. }
  509. const char *_dlerror(void)
  510. {
  511. const char *retval;
  512. if (!_dl_error_number)
  513. return NULL;
  514. retval = dl_error_names[_dl_error_number];
  515. _dl_error_number = 0;
  516. return retval;
  517. }
  518. /*
  519. * Dump information to stderrr about the current loaded modules
  520. */
  521. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  522. void _dlinfo(void)
  523. {
  524. struct elf_resolve *tpnt;
  525. struct dyn_elf *rpnt, *hpnt;
  526. _dl_dprintf(2, "List of loaded modules\n");
  527. /* First start with a complete list of all of the loaded files. */
  528. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  529. _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
  530. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  531. (unsigned) tpnt->symbol_scope,
  532. type[tpnt->libtype],
  533. tpnt->usage_count, tpnt->libname);
  534. }
  535. /* Next dump the module list for the application itself */
  536. _dl_dprintf(2, "\nModules for application (%x):\n",
  537. (unsigned) _dl_symbol_tables);
  538. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  539. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  540. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  541. _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
  542. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  543. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
  544. rpnt->dyn->libname);
  545. }
  546. }
  547. int _dladdr(void *__address, Dl_info * __dlip)
  548. {
  549. struct elf_resolve *pelf;
  550. struct elf_resolve *rpnt;
  551. _dl_map_cache();
  552. /*
  553. * Try and locate the module address is in
  554. */
  555. pelf = NULL;
  556. #if 0
  557. _dl_dprintf(2, "dladdr( %x, %x )\n", __address, __dlip);
  558. #endif
  559. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  560. struct elf_resolve *tpnt;
  561. tpnt = rpnt;
  562. #if 0
  563. _dl_dprintf(2, "Module \"%s\" at %x\n",
  564. tpnt->libname, tpnt->loadaddr);
  565. #endif
  566. if (tpnt->loadaddr < (ElfW(Addr)) __address
  567. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  568. pelf = tpnt;
  569. }
  570. }
  571. if (!pelf) {
  572. return 0;
  573. }
  574. /*
  575. * Try and locate the symbol of address
  576. */
  577. {
  578. char *strtab;
  579. Elf32_Sym *symtab;
  580. int hn, si;
  581. int sf;
  582. int sn = 0;
  583. ElfW(Addr) sa;
  584. sa = 0;
  585. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  586. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  587. sf = 0;
  588. for (hn = 0; hn < pelf->nbucket; hn++) {
  589. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  590. ElfW(Addr) symbol_addr;
  591. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  592. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  593. sa = symbol_addr;
  594. sn = si;
  595. sf = 1;
  596. }
  597. #if 0
  598. _dl_dprintf(2, "Symbol \"%s\" at %x\n",
  599. strtab + symtab[si].st_name, symbol_addr);
  600. #endif
  601. }
  602. }
  603. if (sf) {
  604. __dlip->dli_fname = pelf->libname;
  605. __dlip->dli_fbase = (void *)pelf->loadaddr;
  606. __dlip->dli_sname = strtab + symtab[sn].st_name;
  607. __dlip->dli_saddr = (void *)sa;
  608. }
  609. return 1;
  610. }
  611. }