我有一个从C项目编译的.o
文件,该文件引用了一个名为init_static_pools
的函数。我用objdump -t
显示其符号依赖信息:
00000000 UND 00000000 init_static_pools
根据此thread,UND只是说“ 我需要其他人来提供我该功能”。]
所以我将此.o
文件链接到一个.elf
文件,其中包含init_static_pools
的定义。 objdump -t
显示此文件中的符号为indeed:
00004dcf g F .text 00000048 init_static_pools
据此thread,g
和F
标志表示它是glbal函数。我想这意味着该功能可以静态链接。
我尝试通过以下命令行链接.o
文件和.elf
文件:
/ usr / bin / c ++ -m32 -rdynamic unittest1.o -o unittest1 target.elflib / libgtest.a lib / libgtest_main.a -lpthread
我遇到以下错误:
unittest1.cc :(。text + 0x2d):对`init_static_pools'的未定义引用
该函数仅位于.so文件中,为什么不能将其链接?
这可能与动态链接和静态链接之间不同的符号解析机制有关吗?因为我使用objdump -f
并看到target.elf
是动态对象。如下图所示:
target.elf:文件格式elf32-i386
体系结构:i386,标志0x00000150:
HAS_SYMS,动态,D_PAGED
起始地址0x00001144
基于@ EmployedRussian的评论,我尝试了readelf
。
对于target.elf
,它仅包含1行:
486:00004dcf 72 FUNC GLOBAL DEFAULT 13 init_static_pools
对于unittest1.o
,它包含两行内容:
0000002d 0000fb04 R_386_PLT32 00000000 init_static_pools
和
251:00000000 0 NOTYPE GLOBAL DEFAULT UND init_static_pools
为了完成,它们的标题是:
target.elf
:
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x1144
Start of program headers: 52 (bytes into file)
Start of section headers: 246936 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 9
Size of section headers: 40 (bytes)
Number of section headers: 43
Section header string table index: 42
unittest1.o
:
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 36988 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 40 (bytes)
Number of section headers: 262
Section header string table index: 261
到目前为止,我还不能说出链接失败的根本原因。幸运的是,我刚刚通过[[the series在链接器上找到了Ian Lance Taylor。希望能启发我。但是我想这需要一些时间。
ADD 2-10:33 AM 11/6/2019nm target.elf | grep init_static_pools
nm -D target.elf | grep init_static_pools
就像@EmployedRussian怀疑的那样,第二个命令行没有输出。因此,这意味着下面是一些与target.elf不在其动态符号表中导出init_static_pools,这使得该符号不符合从target.elf外部进行链接的条件。
target.elf
的链接有关的标志:-Wl,-T zephyr/linker.cmd (this is quite long, but it seems to be mostly layout info)
-Wl,-Map=target_prebuilt.map
-Wl,--whole-archive
-Wl,--gc-sections
-Wl,--build-id=none
-Wl,--sort-common=descending
-Wl,--sort-section=alignment
-ldl
-lm
还是我也应该检查编译标志?[我找到了--export-all-symbols,--export-dynamic,--gc-keep-exported选项,我正在尝试它们。
似乎
--export-all-symbols
被忽略了。我想这是针对DLL的。我将
--export-dynamic
和--gc-keep-exported
放在一起,构建可以通过。但是nm -D
仍显示以下消息:target.elf:无符号BTW,如果您对我的问题有所了解,请放一些提示。谢谢!
对于target.elf,它仅包含1行:486: 00004dcf 72 FUNC GLOBAL DEFAULT 13 init_static_pools
不幸的是,这还不足以肯定发生了什么。
为了确定,请运行以下两个命令:
nm target.elf | grep init_static_pools
nm -D target.elf | grep init_static_pools
我怀疑第一个命令会产生输出,而第二个命令不会产生输出。如果是这种情况,则
target.elf
是不是在其动态符号表中导出
关于您最终如何不导出该符号,我只能猜测(因为您没有提供用于链接它的链接命令)。您可能使用了隐藏它的链接脚本。init_static_pools
,这使得该符号不适合从target.elf
外部进行链接。