开发者

In shared library's constructor (_init section), how to know what function is interrupted?

On x86 linux, process A.exe invokes dlopen() to load a sha开发者_运维知识库red library B.so. In B.so, there's a constructor, who wants to know which function in process A.exe is interrupted right before dlopen() is invoked.

How can constructor (_init section) in B.so know?


If I understand your question correctly (the 'interrupting' might be misleading), your application A has several locations that might call dlopen() and you want to know from which of these locations you were called.

First of all, this smells wrong, because a shared library should not be supposed to make any assumptions about who is loading it. If so, you could for instance not run your application in Valgrind, because in this case Valgrind would do the loading instead of the standard dynamic linker and your results might get screwed.

Second, if you really need to do this (why?), then you might take a backtrace in your constructor function. Then search upwards until you find dlopen() and on the next higher stack frame you will find the function that called dlopen.

EDIT: To map the addresses in the stack trace back to functions, you will need debug info of the involved binaries or any other way to map function addresses to symbol names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜