2

我正在玩 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 ();
}

我的类.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;
}

我的测试.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  

为什么我们最后有 count = 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
4

1 回答 1

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 工厂函数,因此匿名命名空间就足够了。这还具有将其余符号标记为非外部符号的效果,这可能有利于库加载时间。

或者,您可以通过仅导出库的客户端需要的符号来在单一定义规则上作弊:

$ 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我们之前看到的符号被标记为私有,因此它们只会解析为同一图像中的符号。

于 2013-06-22T08:17:22.600 回答