-symbolic 和 -shared GCC 标志之间有什么区别?

发布于 2024-08-08 00:24:30 字数 462 浏览 5 评论 0原文

从文档的描述来看,它们似乎做了同样的事情,除了“并非所有系统”支持共享并且“只有某些系统”支持符号(尚不清楚这些是否是同一组系统):

-共享 生成一个共享对象,然后可以将其与其他对象链接起来 形成可执行文件。并非所有系统 支持这个选项。对于可预测的 结果,您还必须指定 与过去相同的一组选项 生成代码(-fpic、-fPIC 或模型 子选项)当您指定此选项时 选项。[1]

-符号 构建共享对象时将引用绑定到全局符号。警告 关于任何未解决的参考文献 (除非被链接编辑器覆盖 选项 -Xlinker -z -Xlinker defs)。 只有少数系统支持此功能 选项。

我怀疑差异在于“生成一个共享对象,然后可以将其与其他对象链接以形成可执行文件”部分,但这听起来对任何库都是如此。这是否意味着生成的共享对象也可以静态链接?

From the documentation's description, they seem to do the same thing except that "not all systems" support shared and "only some systems" support symbolic (it's unclear if these are the same set of systems):

-shared
Produce a shared object which can then be linked with other objects to
form an executable. Not all systems
support this option. For predictable
results, you must also specify the
same set of options that were used to
generate code (-fpic, -fPIC, or model
suboptions) when you specify this
option.[1]

-symbolic
Bind references to global symbols when building a shared object. Warn
about any unresolved references
(unless overridden by the link editor
option -Xlinker -z -Xlinker defs).
Only a few systems support this
option.

I suspect the difference is in the "Produce a shared object which can then be linked with other objects to form an executable" part, but that sounds like something that is true of any library. Does it mean that the resulting shared object can be linked statically too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2024-08-15 00:24:30

摘要: -symbolic 防止共享对象内函数插入

与共享对象的链接允许使用称为符号插入的功能。这个想法是,您可以“插入”全局符号的新定义,以便调用它而不是“常规”定义。

一个典型的例子是 malloc()。在最常见的情况下,malloc() 是在 libc 中定义的。但是您可以通过在加载 libc 之前加载定义该符号的库来插入您自己的 malloc 版本(大多数运行时链接器允许您使用 LD_PRELOAD 到特定库以在可执行文件之前加载)。

默认情况下,共享对象中的任何非静态函数都是全局符号。因此,共享对象中的任何函数都可以被插入。考虑这样一种情况:共享对象具有函数 high_level() 和 low_level(),并且 high_level() 调用 low_level() 作为其实现的一部分,并且 high_level() 和 low_level() 都不是静态函数。

可以插入 low_level() ,以便 high_level() 从不同的共享对象调用 low_level() 。

这就是 -symbolic 的用武之地。创建共享对象时,链接器将看到 low_level() 与 high_level() 定义在同一共享对象中,并绑定该调用,使其无法被插入。这样,您就知道从共享对象中的一个函数到同一共享对象中的另一个函数的任何调用都不会被插入。

Summary: -symbolic prevents intra-shared object function interposition

Linking with shared objects allows for a feature called symbol interposition. The idea is that you can 'interpose' a new definition of a global symbol so that it is called rather then the 'regular' definition.

One classic example is malloc(). In the most common case, malloc() is defined within libc. But you can interpose your own version of malloc by loading a library that defines that symbol before you load libc (most runtime linkers allow you to use LD_PRELOAD to specific libraries to load before the executable).

By default, any function within a shared object that is not static is a global symbol. Because of that, any functions within the shared object can be interposed on. Consider a scenario where a shared object has function high_level() and low_level() and high_level() calls low_level() as part of it's implementation and neither high_level() nor low_level() are static functions.

It's possible to interpose low_level() such that high_level() is calling a low_level() from a different shared object.

This is where -symbolic comes in. When creating your shared object, the linker will see that low_level() is defined in the same shared object as high_level() and bind the call such that it can't be interposed on. This way, you know that any calls from one function in your shared object to another in the same shared object will never be interposed on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文