通过其在 C 中的偏移量调用函数
Call Function By Its Offset In C
我正在尝试动态加载具有dlopen
的共享库,并通过其在库二进制文件中的偏移量调用非导出函数。但是,我无法弄清楚具体如何?不过我知道函数的签名。
void *lib_ref = dlopen("libany.so", RTLD_NOW | RTLD_GLOBAL);
if (lib_ref != NULL) {
const char *(*Ref_0000ABCC)(int *, const char *);
Ref_0000ABCC = dlsym(lib_ref, "0000ABCC");
if (Ref_0000ABCC != NULL) {
int ok;
Ref_0000ABCC(&ok, "Something");
} else {
// could not get reference
}
dlclose(lib_ref);
} else {
// could not load library
}
谁能帮忙?
编辑:我组装了下面的代码,它抛出Fatal signal 11 (SIGSEGV), code 1
:
void *lib_ref = dlopen("libany.so", RTLD_NOW | RTLD_GLOBAL);
if (lib_ref != NULL) {
Dl_info lib_info;
dladdr(lib_ref, &lib_info);
size_t lib_addr = (size_t) lib_info.dli_fbase;
size_t func_addr = lib_addr + 0x0000ABCC;
const char *(*Ref_0000ABCC)(int *, const char *) = (const char *(*)(int *, const char *))(func_addr);
if (Ref_0000ABCC != NULL) {
int ok;
const char *result = Ref_0000ABCC(&ok, "Something");
} else {
// could not find reference
}
dlclose(lib_ref);
} else {
// could not load library
}
如果您正在处理的库libany.so
具有导出的符号,这可能很容易。如果库导出一个名为int some_exported_func(const char *)
的函数,您可以在IDA中检查其偏移量(例如,(我们假设是0x000075AC
。您正在寻找的功能根据 IDA0x0000ABCC
。因此,现在您可以计算这两个偏移量之间的差异,在运行时找到命名函数,然后将该差异添加到其偏移量以获得所需的函数。
示例代码如下:
void *lib_ref = dlopen("libany.so", RTLD_NOW | RTLD_GLOBAL);
if (lib_ref != NULL) {
int (*func_named)(const char *) = dlsym(lib_ref, "some_exported_func");
if (func_named != NULL) {
Dl_info func_info;
dladdr(func_named, &func_info);
size_t addr_named = (size_t) func_info.dli_saddr;
int difference = 0x0000ABCC - 0x000075AC;
size_t addr_ABCC = addr_named + difference;
const char *(*func_ABCC)(int *, const char *) = (const char *(*)(int *, const char *))(addr_ABCC);
if (func_ABCC != NULL) {
int ok;
const char *result = func_ABCC(&ok, "Something");
} else {
// could not find reference
}
}
dlclose(lib_ref);
} else {
// could not load library
}
相关文章:
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 通过指针偏移量访问结构变量值
- 以线程安全的方式转换 C/C++ 中时区名称字符串的时区偏移量
- librdkafka:rd_kafka_assignment 返回分配分区的偏移量 -1001
- 是否通过向封闭对象的地址添加字节偏移量来访问子对象
- 向指针地址添加 20 个字节偏移量
- glMapBufferRange(..) 中的偏移量关系和 glDrawArraysInstanced(..) 中的第一
- 是否有与 C# Structs/StructLayout 等效的功能,C++中的字段偏移量?
- 我的 sumASCII 函数中的此偏移量是多少?
- boost::序列化中的派生类偏移量计算.有效吗?
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 胎面偏移量的时间复杂度?
- 如何从 c++ 中的给定标准输入中获取每个字符的偏移量?
- 编译器在构造函数中计算的成员偏移量不正确
- 使用基地址和偏移量获取变量的地址
- c++ 读取进程内存基址 + 偏移量不起作用
- 通过其在 C 中的偏移量调用函数
- ld:无效的字符串偏移量...对于".strtab"部分
- 调用堆栈字节偏移量
- Python ctype 直接调用具有原始偏移量的函数