简体   繁体   English

链接器如何处理链接到共享库的C ++静态库的唯一typeinfo约束?

[英]How does the linker deal with unique typeinfo constraint for C++ static libraries linked into shared libraries?

I was recently going over calling conventions, ABI specs, etc, and in passing I recall reading about a requirement for executables/libraries to have only one typeinfo for a class. 我最近正在调用约定,ABI规范等,并且顺便提一下,我记得读过有关可执行文件/库只需要一个类的一个typeinfo的要求。

With that in mind, my question is this: suppose two libraries libA.so and libB.so are linked statically against libboost_somethingorother.a ... each will have their own typeinfo for various classes in the boost library. 考虑到这一点,我的问题是:假设两个库libA.solibB.so静态链接到libboost_somethingorother.a ...每个都有自己的类型信息,用于boost库中的各种类。 How does the linker deal with this ambiguous situation? 链接器如何处理这种模糊的情况? Or perhaps a more general question ought be asked: is it safe to statically link C++ libraries into a shared library? 或者也许应该提出一个更普遍的问题:将C ++库静态链接到共享库是否安全?

To give a more concrete example 举一个更具体的例子

// in libC.a
class SomeException : public std::Exception { /* ... */ };

// in libA.so, links statically against libC.a
void A_test() {
  extern void B_test();
  try {
    B_test();
  }
  catch( SomeException ) {}

// in libB.so, links statically against libC.a
void B_test() {
  throw SomeException();
}

Is there anything unsafe about this? 这有什么不安全的吗?

It seems to me it must be doing "the right thing" whatever that is, since objects instantiated from header-only libraries can be safely used across code in 2+ shared libraries. 在我看来,无论是什么都必须做“正确的事情”,因为从仅头文件库实例化的对象可以安全地用于2个以上共享库中的代码。 However, I'm not confident enough in my logic, and I'm rather curious what I may've missed (if anything). 但是,我对自己的逻辑不够自信,而且我很好奇我可能错过了什么(如果有的话)。

I finally got around to tinkering with this today. 我今天终于开始修补这个问题了。 Assuming all conditions are met to emit a typeinfo object, the compiler emits it as a weak symbol. 假设满足所有条件以发出typeinfo对象,编译器将其作为弱符号发出。 Because the symbols are weak, it'll always find the correct typeinfo object. 因为符号很弱,所以它总能找到正确的typeinfo对象。

The compiler does not emit an undefined reference to the typeinfo object (at least, gcc 4.9 & clang 3.5 do not); 编译器不会发出对typeinfo对象的未定义引用(至少,gcc 4.9和clang 3.5不会); it's all or nothing. 这是全有或全无。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM