glibc : Test if lib as DF_1_NODELETE flag or if lib has unique symbol

南笙酒味 提交于 2020-01-24 09:58:05

问题


I'm using dlopen / dlclose to load lib with glibc 2.21.

Is there a C++ call to check a lib as a DF_1_NODELETE flag set ? readelf seems to be able to do it.

or at least if a lib has unique symbol defined in it ? nm is definitely able to do it.

ideally i would like something like :

CloseLib( libHandle lib)
{
  if( checkIfLibIsClosable(lib) )
  {
     dlclose(lib)
  }
}

This is in order to avoid calling dlclose on lib with DF_1_NODELETE flag, as calling it will fails with an assert error :

Inconsistency detected by ld.so: dl-close.c: 764: _dl_close: Assertion `map->l_init_called' failed!

which is caused by DF_1_NODELETE flag set in dl-close.c:762, The flag is set in dl-lookup.c:332

Info about DF_1_NODELETE flag and unique symbol :

DF_1_NODELETE

Unique Symbol


回答1:


This is in order to avoid calling dlclose on lib with DF_1_NODELETE flag, as calling it will fails with an assert error :

Inconsistency detected by ld.so: dl-close.c: 764: _dl_close: \
  Assertion `map->l_init_called' failed!

If calling dlclose on such a library causes above assertion, that is a bug in GLIBC and you should report it in glibc bugzilla.

As for detecting whether the DF_1_NODELETE (or any other) flag, yes you can do it by reading the Elf{32,64}_Ehdr from the start of the library, then reading Elf{32,64}_Phdrs from .e_phoff offset until you find one with .p_type == PT_DYNAMIC, then reading Elf{32,64}_Dyns from its .p_offset until you find one with .d_type == DT_FLAGS, and finally checking whether its .d_un.d_val & DF_1_NODELETE is non-zero.



来源:https://stackoverflow.com/questions/31826309/glibc-test-if-lib-as-df-1-nodelete-flag-or-if-lib-has-unique-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!