在链接上从错误的dylib中拾取的符号

时间:2012-07-10 11:28:23

标签: macos ld dylib

我正在链接一个导入两个相关dylib中定义的符号的二进制文件,而我无法使ld选择正确的符号。

符号为_init_process,它在libSystem.dylib(由ld隐式添加)和libida.dylib(我们的库)中定义。我希望ld选择libida,但我不能让它工作。

这是最后一个链接器命令行:

/usr/libexec/gcc/i686-apple-darwin10/4.2.1/ld -dynamic -dylib
-dylib_compatibility_version 1.0 -dylib_current_version 1.0 -arch
i386 -macosx_version_min 10.5 -macosx_version_min 10.5
-single_module -weak_reference_mismatches non-weak -w -o
../../bin/x86_mac_gcc/plugins/python.pmc -ldylib1.10.5.o
-L../../bin/x86_mac_gcc/
-L/Developer/SDKs/MacOSX10.5.sdk/usr/lib/i686-apple-darwin10/4.2.1
-L/Developer/SDKs/MacOSX10.5.sdk/usr/lib
-L/usr/lib/gcc/i686-apple-darwin10/4.2.1
-L/usr/lib/gcc/i686-apple-darwin10/4.2.1
-L/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../i686-apple-darwin10/4.2.1
-L/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc/i686-apple-darwin10/4.2.1/../../..
-v -lpthread ../../lib/x86_mac_gcc_32/libiconv.2.2.0.dylib
obj/x86_mac_gcc_32/python.o32 obj/x86_mac_gcc_32/idaapi.o32 -lida
-install_name python.pmc -lpython2.6 -ldl -why_load
-search_paths_first -t -lstdc++ -lgcc_s.10.5 -lgcc -lSystem

正如你所看到的,-lida出现在-lSystem之前,所以我希望ld首先选择libida.dylib,但它没有发生:

dlopen(/home/test/build/bin/x86_mac_gcc/plugins/python.pmc): dlopen(/home/test/build/bin/x86_mac_gcc/plugins/python.pmc, 2): Symbol not found: _init_process
  Referenced from: /home/test/build/bin/x86_mac_gcc/plugins/python.pmc
  Expected in: /usr/lib/libSystem.B.dylib
 in /home/test/build/bin/x86_mac_gcc/plugins/python.pmc
/home/test/build/bin/x86_mac_gcc/plugins/python.pmc: can't load file

链接器的调试输出:

Library search paths:
    ../../bin/x86_mac_gcc/
    /Developer/SDKs/MacOSX10.5.sdk/usr/lib/i686-apple-darwin10/4.2.1
    /Developer/SDKs/MacOSX10.5.sdk/usr/lib
    /usr/lib/gcc/i686-apple-darwin10/4.2.1
    /usr/lib/gcc/i686-apple-darwin10/4.2.1
    /Developer/SDKs/MacOSX10.5.sdk/usr/lib/i686-apple-darwin10/4.2.1
    /Developer/SDKs/MacOSX10.5.sdk/usr/lib
    /usr/lib
    /usr/local/lib
Framework search paths:
    /Library/Frameworks/
    /System/Library/Frameworks/
/Developer/SDKs/MacOSX10.5.sdk/usr/lib/dylib1.10.5.o
/Developer/SDKs/MacOSX10.5.sdk/usr/lib/libpthread.dylib
../../lib/x86_mac_gcc_32/libiconv.2.2.0.dylib
obj/x86_mac_gcc_32/python.o32
obj/x86_mac_gcc_32/idaapi.o32
../../bin/x86_mac_gcc//libida.dylib
/usr/lib/libpython2.6.dylib
/Developer/SDKs/MacOSX10.5.sdk/usr/lib/libdl.dylib
/Developer/SDKs/MacOSX10.5.sdk/usr/lib/i686-apple-darwin10/4.2.1/libstdc++.dylib
/Developer/SDKs/MacOSX10.5.sdk/usr/lib/libgcc_s.10.5.dylib
/usr/lib/gcc/i686-apple-darwin10/4.2.1/libgcc.a
/Developer/SDKs/MacOSX10.5.sdk/usr/lib/libSystem.dylib
/usr/lib/system/libmathCommon.A.dylib

1 个答案:

答案 0 :(得分:0)

解决了它。罪魁祸首是-lpthread - libpthread是libSystem的符号链接:

$ ls -la /Developer/SDKs/MacOSX10.5.sdk/usr/lib/libpthread.dylib
lrwxr-xr-x  1 root  wheel  15 Nov  9  2011 /Developer/SDKs/MacOSX10.5.sdk/usr/lib/libpthread.dylib -> libSystem.dylib

在-lida之后移动它之后,一切都按预期工作。