📄 dlib.c
字号:
int _dlclose(void *vhandle){ return do_dlclose(vhandle, 1);}static int do_dlclose(void *vhandle, int need_fini){ struct dyn_elf *rpnt, *rpnt1; struct dyn_elf *spnt, *spnt1; elf_phdr *ppnt; struct elf_resolve *tpnt; int (*dl_elf_fini) (void); void (*dl_brk) (void); struct dyn_elf *handle; unsigned int end; int i = 0; handle = (struct dyn_elf *) vhandle; rpnt1 = NULL; for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) { if (rpnt == handle) { break; } rpnt1 = rpnt; } if (!rpnt) { _dl_error_number = DL_BAD_HANDLE; return 1; } /* OK, this is a valid handle - now close out the file. * We check if we need to call fini () on the handle. */ spnt = need_fini ? handle : handle->next; for (; spnt; spnt = spnt1) { spnt1 = spnt->next; /* We appended the module list to the end - when we get back here, quit. The access counts were not adjusted to account for being here. */ if (spnt == _dl_symbol_tables) break; if (spnt->dyn->usage_count == 1 && spnt->dyn->libtype == loaded_file) { tpnt = spnt->dyn; /* Apparently crt1 for the application is responsible for handling this. * We only need to run the init/fini for shared libraries */ if (tpnt->dynamic_info[DT_FINI]) { dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]); (*dl_elf_fini) (); } } } if (rpnt1) rpnt1->next_handle = rpnt->next_handle; else _dl_handles = rpnt->next_handle; /* OK, this is a valid handle - now close out the file */ for (rpnt = handle; rpnt; rpnt = rpnt1) { rpnt1 = rpnt->next; /* We appended the module list to the end - when we get back here, quit. The access counts were not adjusted to account for being here. */ if (rpnt == _dl_symbol_tables) break; rpnt->dyn->usage_count--; if (rpnt->dyn->usage_count == 0 && rpnt->dyn->libtype == loaded_file) { tpnt = rpnt->dyn; /* Apparently crt1 for the application is responsible for handling this. * We only need to run the init/fini for shared libraries */#if 0 /* We have to do this above, before we start closing objects. * Otherwise when the needed symbols for _fini handling are * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/ if (tpnt->dynamic_info[DT_FINI]) { dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]); (*dl_elf_fini) (); }#endif end = 0; for (i = 0, ppnt = rpnt->dyn->ppnt; i < rpnt->dyn->n_phent; ppnt++, i++) { if (ppnt->p_type != PT_LOAD) continue; if (end < ppnt->p_vaddr + ppnt->p_memsz) end = ppnt->p_vaddr + ppnt->p_memsz; } _dl_munmap(rpnt->dyn->loadaddr, end); /* Next, remove rpnt->dyn from the loaded_module list */ if (_dl_loaded_modules == rpnt->dyn) { _dl_loaded_modules = rpnt->dyn->next; if (_dl_loaded_modules) _dl_loaded_modules->prev = 0; } else for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) if (tpnt->next == rpnt->dyn) { tpnt->next = tpnt->next->next; if (tpnt->next) tpnt->next->prev = tpnt; break; } free(rpnt->dyn->libname); free(rpnt->dyn); } free(rpnt); } dl_brk = (void (*)()) _dl_debug_addr->r_brk; if (dl_brk != NULL) { _dl_debug_addr->r_state = RT_DELETE; (*dl_brk) (); _dl_debug_addr->r_state = RT_CONSISTENT; (*dl_brk) (); } return 0;}const char *_dlerror(){ const char *retval; if (!_dl_error_number) return NULL; retval = dl_error_names[_dl_error_number]; _dl_error_number = 0; return retval;}/* Generate the correct symbols that we need. */#if 0weak_alias(_dlopen, dlopen);weak_alias(_dlerror, dlerror);weak_alias(_dlclose, dlclose);weak_alias(_dlsym, dlsym);weak_alias(_dladdr, dladdr);#endifasm(".weak dlopen;dlopen=_dlopen");asm(".weak dlerror;dlerror=_dlerror");asm(".weak dlclose;dlclose=_dlclose");asm(".weak dlsym;dlsym=_dlsym");asm(".weak dladdr;dladdr=_dladdr");/* This is a real hack. We need access to the dynamic linker, but wealso need to make it possible to link against this library without anyunresolved externals. We provide these weak symbols to make the linkpossible, but at run time the normal symbols are accessed. */static void __attribute__ ((unused)) foobar(){ _dl_dprintf(2, "libdl library not correctly linked\n"); _dl_exit(1);}asm(".weak _dl_dprintf; _dl_dprintf = foobar");asm(".weak _dl_find_hash; _dl_find_hash = foobar");asm(".weak _dl_load_shared_library; _dl_load_shared_library = foobar");asm(".weak _dl_parse_relocation_information; _dl_parse_relocation_information = foobar");asm(".weak _dl_parse_lazy_relocation_information; _dl_parse_lazy_relocation_information = foobar");#ifdef USE_CACHEasm(".weak _dl_map_cache; _dl_map_cache = foobar");asm(".weak _dl_unmap_cache; _dl_unmap_cache = foobar");#endif #if 0weak_alias(_dl_dprintf, foobar);weak_alias(_dl_find_hash, foobar);weak_alias(_dl_load_shared_library, foobar);weak_alias(_dl_parse_relocation_information, foobar);weak_alias(_dl_parse_lazy_relocation_information, foobar);#ifdef USE_CACHEweak_alias(_dl_map_cache, foobar);weak_alias(_dl_unmap_cache, foobar);#endif #endif static int __attribute__ ((unused)) foobar1 = (int) foobar; /* Use as pointer */asm(".weak _dl_symbol_tables; _dl_symbol_tables = foobar1");asm(".weak _dl_handles; _dl_handles = foobar1");asm(".weak _dl_loaded_modules; _dl_loaded_modules = foobar1");asm(".weak _dl_debug_addr; _dl_debug_addr = foobar1");asm(".weak _dl_error_number; _dl_error_number = foobar1");asm(".weak _dl_malloc_function; _dl_malloc_function = foobar1");#if 0weak_alias(_dl_symbol_tables, foobar1);weak_alias(_dl_handles, foobar1);weak_alias(_dl_loaded_modules, foobar1);weak_alias(_dl_debug_addr, foobar1);weak_alias(_dl_error_number, foobar1);weak_alias(_dl_malloc_function, foobar1);#endif/* * Dump information to stderrr about the current loaded modules */static char *type[] = { "Lib", "Exe", "Int", "Mod" };void _dlinfo(){ struct elf_resolve *tpnt; struct dyn_elf *rpnt, *hpnt; _dl_dprintf(2, "List of loaded modules\n"); /* First start with a complete list of all of the loaded files. */ for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) { _dl_dprintf(2, "\t%x %x %x %s %d %s\n", (unsigned) tpnt->loadaddr, (unsigned) tpnt, (unsigned) tpnt->symbol_scope, type[tpnt->libtype], tpnt->usage_count, tpnt->libname); } /* Next dump the module list for the application itself */ _dl_dprintf(2, "\nModules for application (%x):\n", (unsigned) _dl_symbol_tables); for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname); for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) { _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt); for (rpnt = hpnt; rpnt; rpnt = rpnt->next) _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname); }}int _dladdr(void *__address, Dl_info * __dlip){ struct elf_resolve *pelf; struct elf_resolve *rpnt;#ifdef USE_CACHE _dl_map_cache();#endif /* * Try and locate the module address is in */ pelf = NULL;#if 0 _dl_dprintf(2, "dladdr( 0x%p, 0x%p )\n", __address, __dlip);#endif for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) { struct elf_resolve *tpnt; tpnt = rpnt;#if 0 _dl_dprintf(2, "Module \"%s\" at 0x%p\n", tpnt->libname, tpnt->loadaddr);#endif if (tpnt->loadaddr < (char *) __address && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) { pelf = tpnt; } } if (!pelf) { return 0; } /* * Try and locate the symbol of address */ { char *strtab; Elf32_Sym *symtab; int hn, si; int sf; int sn = 0; void *sa = 0; symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr); strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr); sf = 0; for (hn = 0; hn < pelf->nbucket; hn++) { for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) { void *symbol_addr; symbol_addr = pelf->loadaddr + symtab[si].st_value; if (symbol_addr <= __address && (!sf || sa < symbol_addr)) { sa = symbol_addr; sn = si; sf = 1; }#if 0 _dl_dprintf(2, "Symbol \"%s\" at 0x%p\n", strtab + symtab[si].st_name, symbol_addr);#endif } } if (sf) { __dlip->dli_fname = pelf->libname; __dlip->dli_fbase = pelf->loadaddr; __dlip->dli_sname = strtab + symtab[sn].st_name; __dlip->dli_saddr = sa; } return 1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -