【发布时间】:2020-05-02 16:59:21
【问题描述】:
在main.c下面给出:
#include <stdio.h>
void test()
{
printf("test()\n");
}
int main() {
test();
return 0;
}
执行以下命令:
clang-10 main.c -o main
readelf -s main
插入输出复制到这里:
Symbol table '.dynsym' contains 4 entries:
... ignore ...
Symbol table '.symtab' contains 62 entries:
Num: Value Size Type Bind Vis Ndx Name
61: 00000000004004f0 23 FUNC GLOBAL DEFAULT 13 test
问题:
- 其他可重定位(目标文件或静态库)或共享库或可执行文件是否可以使用/链接/访问 .symtab 表中的 test 符号?
注意:感谢您查看问题,这个问题仅用于教育目的,我没有遇到这个问题。
编辑: 导出可执行动态表中的test符号:
clang-10 main.c -Wl,--dynamic-list=symbols.txt -fPIC -o main
symbols.txt:
{
test;
};
test 符号显示在 .dymsym 表中。
从下面的源文件(shared.c)构建另一个共享库,依赖于上面的可执行文件main:
extern void test();
void share() {
test();
}
构建命令:
clang-10 shared.c main -fPIC -shared -o libShared.so
但是,构建失败并给出以下错误消息:
main: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
main: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtbeginS.o:(.data.rel.local+0x0): first defined here
main: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtendS.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
main:(.data+0x10): first defined here
/usr/bin/ld: error in main(.eh_frame); no .eh_frame_hdr table will be created.
【问题讨论】: