是什么让dylib与另一个不同?

时间:2013-06-20 15:56:15

标签: c++ macos dylib dyld

我正在使用dylib多次加载,并尝试了解使符号不同的原因。

以下是我的步骤:

  • 使用以下入口点构建lib_a.dylib:

    FactoryA : IFActory () {}
    extern "C" IFactory* GetFactory () { return new FactoryA(); }
    
  • 将lib_a.dylib复制到lib_b.dylib
  • 从lib_a.dylib和lib_b.dylib加载GetFactory

    void * module=dlopen(fileName,RTLD_LAZY);
    void * proc = (void *)dlsym(module, "GetFactory");        
    

加载第二个dylib(lib_b.dylib)时,GetFactory被视为已由lib_a.dylib定义。
实际上,nm输出具有相同的结果。

但是我认为编译标签-two_level_namespace保证2 dylib在一个不同的命名空间中,我错了吗?

如果我的两个dylib已加载,我可以更改哪些内容?


以下是我的测试。

myclass.h:

#include <stdio.h>  
class IFactory {  
public:  
    virtual int GetCount() = 0;  
};  
extern "C"  
{
    extern IFactory* GetFactory ();
}

myclass.cpp

#include <stdio.h>
#include "myclass.h"

class MyFactory : public IFactory {
public:
    virtual int GetCount() { mCount++; return mCount; }
    static int mCount;
};
int MyFactory::mCount = 0;


IFactory* GetFactory () {
    return new MyFactory;
}

mytest.cpp

#include <stdio.h>
#include <dlfcn.h>
#include <mach-o/dyld.h>
#include "myclass.h"

typedef IFactory* (*factoryPtr)();

int main()
{  
    void* handleA = dlopen("libmylib.dylib", RTLD_LAZY);
    factoryPtr functionA = (IFactory*(*)())dlsym(handleA, "GetFactory");
    IFactory*  factoryA = (*functionA)();
    fprintf(stderr, "Handle A: %p\tFunction A: %p\t Count: %d\n", handleA, functionA, factoryA->GetCount());

    // Reload same library
    void* handleB = dlopen("libmylib.dylib", RTLD_LAZY);
    factoryPtr functionB = (IFactory*(*)())dlsym(handleB, "GetFactory");
    IFactory*  factoryB = (*functionB)();
    fprintf(stderr, "Handle B: %p\tFunction B: %p\t Count: %d\n", handleB, functionB, factoryB->GetCount());

    // Load copy of first library (just rename)
    void* handleC = dlopen("libmylib_copy.dylib", RTLD_LAZY);
    factoryPtr functionC = (IFactory*(*)())dlsym(handleC, "GetFactory");
    IFactory*  factoryC = (*functionC)();
    fprintf(stderr, "Handle C: %p\tFunction C: %p\t Count: %d\n", handleC, functionC, factoryC->GetCount());

    return 0;
}   

命令:

clang++ -dynamiclib myclass.cpp -o libmylib.dylib  
cp libmylib.dylib libmylib_copy.dylib  
clang++ mytest.cpp -o mytest  
./mytest

输出:

Handle A: 0x7fe5dac039b0    Function A: 0x106d49d30  Count: 1    
Handle B: 0x7fe5dac039b0    Function B: 0x106d49d30  Count: 2    
Handle C: 0x7fe5dac03e00    Function C: 0x106d7cd30  Count: 3  

为什么我们最后有数= 3?


属性-fvisibility = hidden -fvisibility-inlines-hidden允许执行相同的操作。

修改myclass.h:

#include <stdio.h>  
#define EXPORT_FACTORY  __attribute__ ((visibility ("default")))
class IFactory {  
public:  
    virtual int GetCount() = 0;  
};   
extern "C"  
{
    extern EXPORT_FACTORY IFactory* GetFactory ();
}

构建:

clang++ -dynamiclib myclass.cpp  -o libmylib.dylib  -fvisibility=hidden -fvisibility-inlines-hidden
cp libmylib.dylib libmylib_copy.dylib
clang++ mytest.cpp -o mytest
./mytest

输出:

Handle A: 0x7fe078c039b0    Function A: 0x1076e1c00  Count: 1
Handle B: 0x7fe078c039b0    Function B: 0x1076e1c00  Count: 2  
Handle C: 0x7fe078c03e20    Function C: 0x107714c00  Count: 1

1 个答案:

答案 0 :(得分:1)

这里的根本问题是库中符号的可见性。您可以通过nm -m看到除了您要导出的GetFactory函数之外还有几个外部符号:

$ nm -m libmylib.dylib
0000000000000f30 (__TEXT,__text) external _GetFactory
0000000000001068 (__DATA,__common) external __ZN9MyFactory6mCountE
0000000000000f50 (__TEXT,__text) weak external __ZN9MyFactory8GetCountEv
0000000000001038 (__DATA,__data) weak external __ZTI8IFactory
0000000000001050 (__DATA,__data) weak external __ZTI9MyFactory
0000000000000f91 (__TEXT,__const) weak external __ZTS8IFactory
0000000000000f86 (__TEXT,__const) weak external __ZTS9MyFactory
0000000000001020 (__DATA,__data) weak external __ZTV9MyFactory
                 (undefined) external __ZTVN10__cxxabiv117__class_type_infoE (from libc++)
                 (undefined) external __ZTVN10__cxxabiv120__si_class_type_infoE (from libc++)
                 (undefined) weak external __Znwm (from libc++)
                 (undefined) external dyld_stub_binder (from libSystem)

标记为weak external的符号是导致问题的原因。

使用DYLD_PRINT_BINDINGS=YES运行测试应用程序会显示:

$ DYLD_PRINT_BINDINGS=YES ./mytest
[ … output showing initialization of libstdc++.dylib and libmylib.dylib omitted …]
Handle A: 0x7fc729c03810    Function A: 0x102a51ee0  Count: 1
Handle B: 0x7fc729c03810    Function B: 0x102a51ee0  Count: 2
dyld: bind: libmylib_copy.dylib:0x102A85038 = libc++abi.dylib:__ZTVN10__cxxabiv117__class_type_infoE, *0x102A85038 = 0x7FFF7CA67B50 + 16
dyld: bind: libmylib_copy.dylib:0x102A85050 = libc++abi.dylib:__ZTVN10__cxxabiv120__si_class_type_infoE, *0x102A85050 = 0x7FFF7CA67BD0 + 16
dyld: bind: libmylib_copy.dylib:0x102A85018 = libstdc++.6.dylib:__Znwm, *0x102A85018 = 0x7FFF938F0325
dyld: bind: libmylib_copy.dylib:0x102A85000 = libdyld.dylib:dyld_stub_binder, *0x102A85000 = 0x7FFF9084E878
dyld: weak bind: libmylib_copy.dylib:0x102A85030 = libmylib.dylib:__ZN9MyFactory8GetCountEv, *0x102A85030 = 0x102A51F00
dyld: weak bind: libmylib_copy.dylib:0x102A85060 = libmylib.dylib:__ZTI8IFactory, *0x102A85060 = 0x102A52038
dyld: weak bind: libmylib_copy.dylib:0x102A85028 = libmylib.dylib:__ZTI9MyFactory, *0x102A85028 = 0x102A52050
dyld: weak bind: libmylib_copy.dylib:0x102A85040 = libmylib.dylib:__ZTS8IFactory, *0x102A85040 = 0x102A51F41
dyld: weak bind: libmylib_copy.dylib:0x102A85058 = libmylib.dylib:__ZTS9MyFactory, *0x102A85058 = 0x102A51F36
dyld: weak bind: libmylib_copy.dylib:0x102A85010 = libmylib.dylib:__ZTV9MyFactory, *0x102A85010 = 0x102A52020
dyld: weak bind: libmylib_copy.dylib:0x102A85018 = libstdc++.6.dylib:__Znwm, *0x102A85018 = 0x7FFF938F0325
Handle C: 0x7fc729c03c20    Function C: 0x102a84ee0  Count: 3

如果您查看weak bind行,您会看到我们之前看到的标记为weak external的符号正在解析为libmylib.dylib中同名的符号。我认为这种行为与C ++的一个定义规则有关(“每个程序应该只包含该程序中使用的每个非内联函数或对象的一个​​定义”)。由于程序中有相同非内联函数的多个定义,因此链接器尝试在加载时合并符号,因此只使用单个函数。

这里最好的解决方案是每个库将其符号放在不同的命名空间中。由于您只想导出C工厂函数,因此匿名命名空间就足够了。这也具有将符号的其余部分标记为非外部的效果,这对于库加载时间是有益的。

或者,您可以通过仅导出库的客户端所需的符号来欺骗One Definition Rule:

$ clang++ -Wl,-exported_symbol -Wl,_GetFactory -dynamiclib myclass.cpp -o libmylib.dylib
$ cp libmylib.dylib libmylib_copy.dylib  
$ ./mytest
Handle A: 0x7fc593403910    Function A: 0x1009e4e90  Count: 1
Handle B: 0x7fc593403910    Function B: 0x1009e4e90  Count: 2
Handle C: 0x7fc593403b10    Function C: 0x1009e7e90  Count: 1

这会导致我们之前看到的weak external符号被标记为私有,因此它们只会解析为同一图像中的符号。