Clang / GCC是否真的支持延迟加载功能? [英] Does Clang/GCC really support a delay loading feature?

查看:150
本文介绍了Clang / GCC是否真的支持延迟加载功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您的真实体验涉及上述标题,您是否愿意留下您的评论?我试图让一个共享对象延迟加载在Ubuntu上的Clang和GCC(我其实并不介意使用哪种编译器),但他们看起来并不真正支持任何延迟加载特性(我预计延迟加载特性将一个存根放在一个父对象中,该对象试图按需加载另一个对象,此时需要该功能,但实际上并未这样做)。以下命令显示我试图将libbar.so延迟加载到libfoo.so:

  clang bar.c -fPIC -shared -o libbar.so 
clang foo.c -Wl,-zlazy,lL'/ path / to / where / lib / is', - lbar -o foo

如果libbar.so不存在,您会在进入条目之前看到libfoo.so引发异常。无论如何,我不介意上面的命令是否有错字,但想知道 Clang / GCC确实支持延迟加载功能



但是,我个人不能相信,如果Clang / GCC不支持任何延迟加载特性,Linux程序开发人员是否需要调用dlopen()或dlsym()来使共享对象延迟加载。如果对象是用C编写的,但是如果用C ++编写的话,情况可能会好起来的,情况必然是非常复杂的:($ / b>

我相信一个解决方案已经实现了在编译器或链接器的帮助下是最好的,因为我已经在Windows和Mac OS上成功地完成了它,所以我觉得这将是一个自然的反应,即公民想要在Clang / GCC上拥有延迟加载功能,如果您对我的感受有任何评论,我也会很感激。



PS。我知道Solaris支持延迟加载功能,但这不是我的选择,因为我会不要开发任何东西。



无论如何,非常感谢您提前致谢。

解决方案

这更多的是由运行时链接器提供的功能问题, ld-linux.so



此链接器支持符号的延迟绑定,但不支持延迟加载lib这意味着当程序启动时,可执行文件需要的每个共享对象都会被加载,但是程序中的符号在被第一次引用之前不会被解析到加载的库中。



原因是表现。一个库可能包含成千上万的函数符号,这些函数在单个程序执行过程中永远不会被调用。解决它们都是浪费时间。因为这个原因,如果一个库不包含预期的符号,你可以在程序开始运行后得到'未定义的符号'的错误,但是如果一个库是



您正在引用控件的 -zlazy 选项只有懒符号绑定。实际上,它是默认启用的(至少对于GCC,我没有检查clang)。



例如在程序启动后加载库的唯一方法,例如响应某些命令行选项,配置或其他动态条件,请致电 dlopen



你可能想要寻找一个好的插件框架 - 参考见:


Would you mind to leave your comment on this if you have really experienced which relates to the title above? I have tried to make a shared object to be delay loaded with both Clang and GCC on Ubuntu (I actually don't mind which compiler is used), but they do not look really support any delay loading feature (I expected the delay loading feature put a stub in a parent object, which was trying to load another object on demand, at a moment when the functionality is required, but it actually did not). The following commands show that I tried to make libbar.so to be delay loaded against to libfoo.so:

clang bar.c -fPIC -shared -o libbar.so
clang foo.c -Wl,-zlazy,lL'/path/to/where/lib/is',-lbar -o foo

You'll see the libfoo.so raise an exception before entering to the entry if libbar.so does not exist. Anyway, I don't mind if there were any typo in the commands above, but want to know Clang/GCC really supports a delay loading feature or not.

Personally, however, I can't believe if Linux program developers have been required to invoke dlopen() or dlsym() to make a shared object to be delay loaded if Clang/GCC did not support any delay loading feature. It could be okay if the object was written in C, but if it were written in C++, the situation must be completely complicated :(

I believe a solution which is realized with a help from compiler or linker is the best because I have successfully done it with Windows and Mac OS. So I feel it would be a natural reaction where citizen wants to dream to have a delay loading feature even on Clang/GCC. I'd also be appreciate if you have any comment on my feeling.

PS. I know Solaris supports a delay loading feature but that's not a way to go for me because I will don't develop anything on it.

Anyway, thank you very much in advance.

解决方案

This is more a question of functionality provided by the run time linker, ld-linux.so.

This linker does support lazy binding of symbols, but not lazy loading of libraries. What this means is that each of the shared objects which an executable requires are loaded when the program starts, but the symbols within the program are not resolved to the loaded libraries until they are first referenced.

The reason for this is performance. A library may contain many thousands of symbols for functions that never get called in a single execution of a program. Resolving them all would be a waste of time.

For this reason, if a library does not contain the expected symbols, you can get 'undefined symbol' errors well after the program has started running, but if a library is missing altogether, you will get an error before the program starts.

The -zlazy option which you are quoting controls lazy symbol binding only. In fact it is enabled by default (at least for GCC, I did not check for clang).

The only way to have a library loaded after program startup, for example in response to some command line option, configuration or other dynamic condition, is to call dlopen.

You might want to look around for a good plugin framework - for references see:

这篇关于Clang / GCC是否真的支持延迟加载功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆