即使“-nostdlib”不存在,如何运行构造函数选项已定义
我有一个包含构造函数的动态库。
__attribute__ ((constructor))
void construct() {
// This is initialization code
}
该库是使用 -nostdlib 选项编译的,我无法更改它。因此,库中没有 .ctor
和 .dtor
部分,并且构造函数不会在库加载时运行。
正如所写那里应该即使在这种情况下也可以采取特殊措施来允许运行构造函数。您能告诉我该怎么做以及如何做吗?
I have a dynamic library that contains a constructor.
__attribute__ ((constructor))
void construct() {
// This is initialization code
}
The library is compiled with -nostdlib
option and I cannot change that. As a result there are no .ctor
and .dtor
sections in library and the constructor is not running on the library load.
As written there there should be special measures that allow running the constructor even in this case. Could you please advice me what and how that can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么需要构造函数?与我一起工作的大多数程序员(包括我自己)都拒绝使用具有全局构造函数的库,因为它们常常会在输入
main
时弄乱程序的初始状态,从而引入错误。我能想到的一个具体例子是OpenAL
,它在仅仅链接时就破坏了程序,即使它从未被调用。我不是该项目中处理此错误的人,但如果我没记错的话,它与对 ALSA 的破坏以及后来破坏主程序对 ALSA 的使用有关。如果您的库具有重要的全局状态,请查看是否可以简单地使用全局结构和初始值设定项。不过,您可能需要添加带有一些指针的标志,以指示它们是指向分配的内存还是静态内存。另一种方法是将初始化推迟到第一次调用,但这可能会产生线程安全问题,除非您使用 pthread_once 或类似的方法。
Why do you need constructors? Most programmers I work with, myself included, refuse to use libraries with global constructors because all too often they introduce bugs by messing up the program's initial state when
main
is entered. One concrete example I can think of isOpenAL
, which broke programs when it was merely linked, even if it was never called. I was not the one on the project who dealt with this bug, but if I'm not mistaken it had something to do with mucking with ALSA and breaking the main program's use of ALSA later.If your library has nontrivial global state, instead see if you can simply use global structs and initializers. You might need to add flags with some pointers to indicate whether they point to allocated memory or static memory, though. Another method is to defer initialization to the first call, but this can have thread-safety issues unless you use
pthread_once
or similar.嗯,错过了没有 .ctor 和 .dtor 部分的部分......忘记这一点。
这给出了:
如果您在大端系统上运行,可能您需要交换继续和中断,但我不完全确定。
但就像 R.. 所说,在库中使用静态构造函数对于使用库的开发人员来说并不是那么好:p
Hmm missed the part that there where no .ctor and .dtor sections... forget about this.
Which gives:
Probably you need to swap the continue and break if you are running on an Big Endian system but i'm not entirely sure.
But just like R.. stated using static constructors in libraries is not so nice to the developers using your library :p
在某些平台上,生成
.init_array/.fini_array
部分以包含所有全局构造函数/析构函数。你可以用它。On some platforms,
.init_array/.fini_array
sections are generated to include all global constructors/destructors. You may use that.