Linux下动态加载的库的地址范围 [英] Address range of a dynamically loaded library under Linux
问题描述
我有一个工作程序,可使用 dlopen
加载插件。
I have a working program that loads plugins with dlopen
.
新要求:代码,我得到了一个指针,我需要测试该指针是否指向插件的代码或静态数据。
New requirement: at some point in the code, I'm given a pointer, and I need to test whether this pointer points into the code or static data of a plugin.
bool is_pointer_into_plugin(void *p, void *handle);
等效地,我需要检索指针指向的插件(如果有)。我还需要知道指针是否指向主程序的代码或静态数据(理想情况下,区分只读和读写区域)。
Equivalently, I need to retrieve the plugin into which a pointer points, if any. I also need to know if the pointer points into the main program's code or static data (and ideally, distinguish between read-only and read-write areas).
void *handle plugin_containing_pointer(void *p);
等效地,我需要能够检索插件所在的范围(地址和大小)映射。我也需要主程序使用此信息。
Equivalently, I need to be able to retrieve the extent (address and size) at which a plugin is mapped. I also need this information for the main program.
如何实现 is_pointer_into_plugin
或 plugin_ contains_pointer
或等效的东西?
How can I implement is_pointer_into_plugin
, or plugin_containing_pointer
, or something equivalent?
我可以将调用更改为 dlopen
必要。查找应尽可能快,加载时间代码不必太快。在单独的进程中运行插件并通过共享内存进行通信是不可能的。
I can change the call to dlopen
if necessary. The lookup should be reasonably fast as possible, the load-time code doesn't need to be fast. Running the plugins in separate processes and communicating through shared memory is not an option.
我的程序在Linux(和Windows)下运行,但这是另一个问题)。将来可移植到其他unix系统(至少是OSX)将是一个加号。
My program runs under Linux (and Windows, but that's another question). Future portability to other unix systems (at least to OSX) would be a plus.
推荐答案
一种方法,如果您控制插件足够-使用链接程序脚本应该可以解决问题-将确保在开始和结束处都有指定的符号-即类似于 __ executable_start
, _etext
, _edata
, _end
-然后使用dlsym获取其地址。您可能会在具有动态库的所有系统上获得相同的效果,从而将大多数系统依赖关系推到了构建方面。
One way, if you control the plugin enough -- playing with linker scripts should do the trick --, would be to ensure having specified symbols at start and end -- i.e. similar to __executable_start
, _etext
, _edata
, _end
-- and then use dlsym to get their addresses. You can probably get the same effect on all systems with dynamic libraries, thus pushing most the system dependencies on the build side.
这篇关于Linux下动态加载的库的地址范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!