【发布时间】:2021-10-16 01:24:47
【问题描述】:
我有一个动态链接库,它定义了我需要访问的__attribute__((visibility("hidden"))) 符号。这是一个简化的代码
shared.c
__attribute__((visibility("hidden"))) int hidden_sym[12];
int visible_sym[12];
shared_user.c
#include <dlfcn.h>
#include <stdio.h>
int main() {
void* dlopen_res = dlopen("./libnogcc.so", RTLD_LAZY);
if (dlopen_res == NULL) {
printf("dlopen_res is NULL: %s\n", dlerror());
return 1;
}
if (dlsym(dlopen_res, "visible_sym") == NULL) {
printf("bb_so is NULL: %s\n", dlerror());
return 1;
} else {
printf("'visible_sym' open ok\n");
}
if (dlsym(dlopen_res, "hidden_sym") == NULL) {
printf("bb_so is NULL: %s\n", dlerror());
return 1;
}
}
compilation and execution
gcc shared.c -fpic -shared -olibnogcc.so
gcc -ldl shared_user.c -o shared_main
./shared_main
它正确加载了visible_symbol,但预计无法解析隐藏符号:
'visible_sym' open ok
bb_so is NULL: ./libnogcc.so: undefined symbol: hidden_sym
我想知道是否有任何解决方法可以让我访问隐藏符号。
请注意,它不需要是基于dlsym 的解决方案。在不修改库符号表的情况下,任何可以让我访问隐藏符号的东西都将被视为可接受的解决方案。
我的实际用例非常相似 - 我想访问由gprof 在检测代码中生成的分析信息。我仍然不确定,但它似乎存储在声明为struct __bb *__bb_head __attribute__((visibility("hidden"))); 的__bb_head 变量中。使用<sys/gmon.h> 和<sys/gmon_out.h> 标头可以访问结构定义,但我无法找到任何方法来实际获取原始形式的分析数据。我知道gprof 允许我在程序完成执行时转储信息,但我需要在运行时获取这些数据,而不必强制文件写入然后重新读取。
code for accessing libc data
#include <dlfcn.h>
#include <stdio.h>
#include <sys/gmon.h>
#include <sys/gmon_out.h>
int main() {
void* dlopen_res = dlopen("libc.so.6", RTLD_LAZY);
if (dlopen_res == NULL) {
printf("dlopen_res is NULL: %s\n", dlerror());
return 1;
}
void* bb_so = dlsym(dlopen_res, "__bb_head");
if (bb_so == NULL) {
printf("bb_so is NULL: %s\n", dlerror());
return 1;
}
}
【问题讨论】:
-
动态符号表中不存在隐藏符号,所以恐怕你在这里不走运。
-
除非你能得到一个偏移量并通过一些技巧来计算地址。
-
那是可悲的。好吧,如果我尝试以某种方式替换分析调用并自己收集信息,也许我的运气会更好,但这几乎与通过偏移/地址进行黑客攻击一样糟糕。
-
隐藏符号对共享库来说是全局的吗?也就是说,它是否与共享库中的其他对象有外部链接?
-
无论如何,如果您了解某个函数正在使用的符号,并且该函数未隐藏,则可以使用调试器确定如何访问隐藏的符号。如果您能够重建库,您可以简单地暴露隐藏的符号。如果符号声明为
static,则它按设计运行。
标签: c linux gcc shared-libraries dynamic-library