OS X上的弱符号别名类似于Linux上的别名,还是最接近的别名? [英] Weak symbol aliases on OS X similar to those on Linux, or a closest equivalent?

查看:102
本文介绍了OS X上的弱符号别名类似于Linux上的别名,还是最接近的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做什么

在为Linux编写共享库时,我倾向于注意重定位,符号可见性,GOT/PLT等.

When writing shared libraries for Linux, I tend to pay attention to relocations, symbol visibility, GOT/PLT etc.

在适用的情况下,当来自同一库的函数相互调用时,我试图避免调用PLT存根.例如,假设一个共享库提供了两个公共函数-foo()bar()(这两个函数都可以由用户调用).但是,bar()函数也调用foo().因此,在这种情况下,我要做的是:

When applicable, I am trying to avoid calling PLT stubs when functions from the same library call each other. For example, let's say a shared object provides two public functions - foo() and bar() (either of those can be called by user). The bar() function, however, also calls foo(). So what I do in this case is this:

  1. 定义具有私人可见性的_foo()_bar()函数.
  2. 分别为_foo()_bar()定义foo()bar()弱别名.
  1. Define _foo() and _bar() functions that have private visibility.
  2. Define foo() and bar() weak aliases for _foo() and _bar() respectively.

这样,共享库中的代码就永远不会使用弱符号.它仅直接调用本地函数.例如,调用_bar()时,它将直接调用_foo().

That way, the code in shared object never uses weak symbols. It only invokes local functions, directly. For example, when _bar() is invoked, it calls _foo() directly.

但是用户不了解_*功能,并且始终使用相应的弱别名.

But users are not aware of _* functions and always use corresponding weak aliases.

我该怎么做

在Linux中,这是通过使用以下结构实现的:

In Linux, this is achieved by using the following construct:

extern __typeof (_NAME) NAME __attribute__(weak, alias("_NAME"));

问题

不幸的是,这不适用于OSX.我对OS X或其二进制格式没有很深的了解,所以我仔细研究了一下,发现了一些弱函数示例(例如

Unfortunately, this does not work for OS X. I have no deep knowledge of OS X or its binary formats, so I poked around a bit and found a few examples of weak functions (like this one), but those don't quite do the same as you can have a weak symbol, but not a weak symbol that is an alias for DSO's local function.

可能的解决方案...

现在,我刚刚禁用了此功能(使用宏实现),以使所有符号都是全局符号并具有默认可见性.我现在想到的唯一方法是使所有_foo函数具有私有可见性,并具有相应的foo函数具有默认可见性,并将它们称为隐藏"对应物.

For now, I have just disabled this feature (that is implemented using macros) so that all symbols are global and have default visibility. The only way I can think of to achieve the same for now is to have all _foo functions with private visibility and have corresponding foo functions with default visibility and calling their "hidden" counterparts.

更好的方法?

但是,这需要更改很多代码.因此,我宁愿不要去那里,除非真的没有其他方法.

That, however, requires a good chunk of code to be changed. Therefore I would prefer not to go there unless there is really no other way.

那么封闭OS X的替代方法是什么?获得相同语义/行为的最简单方法是什么?

So what is the closes OS X alternative or the easiest way to get the same semantics/behavior?

推荐答案

在OS X上,在库中进行的调用是自动直接调用,不会通过dyld存根.事实证明,如果您希望能够注入替代功能来为调用提供服务,则需要使用interposable来强制间接访问符号并通过dyld存根强制执行调用.否则,默认情况下,本地调用将是直接的,并且不会产生通过dyld运行的开销.

On OS X, calls made within the library are automatically direct calls and do not go through the dyld stub. The evidence to the fact is that if you want to be able to inject alternative functions to service a call, you'll need to use interposable to force indirect access to the symbols and force execution of the call through the dyld stubs. Otherwise, by default, local calls will be direct and will not incur the overhead of running through dyld.

因此,您在Linux上的优化已经是默认行为,并且不需要别名.

Thus, your optimization on Linux is already the default behavior and the alias is not needed.

不过,如果您要这样做只是为了简化与平台兼容的代码,则仍然可以使用别名.您只需使用"weak_import"或"weak"(如果要合并)作为属性名称.

Still, if you want to do this just to make your platform compatible code simpler, you can still make the aliases. You just need to use "weak_import" or "weak" (if you want coalesced) as your attribute name.

外部 typeof(_NAME)NAME __attribute (弱导入,别名("_NAME"));

extern typeof (_NAME) NAME __attribute(weak_import, alias("_NAME"));

Apple关于弱链接的参考:标记符号弱链接
关于Mach-O运行时绑定的Apple参考:

Apple reference on Weak Linking: Marking Symbols for Weak Linking
Apple reference on Mach-O runtime binding : Scope and Treatment of Symbol Definitions

这篇关于OS X上的弱符号别名类似于Linux上的别名,还是最接近的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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