c - Weak symbol aliases on OS X similar to those on Linux, or a closest equivalent? -
what do
when writing shared libraries linux, tend pay attention relocations, symbol visibility, got/plt etc.
when applicable, trying avoid calling plt stubs when functions same library call each other. example, let's shared object provides 2 public functions - foo()
, bar()
(either of can called user). bar()
function, however, calls foo()
. in case this:
- define
_foo()
,_bar()
functions have private visibility. - define
foo()
,bar()
weak aliases_foo()
,_bar()
respectively.
that way, code in shared object never uses weak symbols. invokes local functions, directly. example, when _bar()
invoked, calls _foo()
directly.
but users not aware of _*
functions , use corresponding weak aliases.
how it
in linux, achieved using following construct:
extern __typeof (_name) name __attribute__(weak, alias("_name"));
the problem
unfortunately, not work os x. have no deep knowledge of os x or binary formats, poked around bit , found few examples of weak functions (like this one), don't quite same can have weak symbol, not weak symbol alias dso's local function.
possible solution...
for now, have disabled feature (that implemented using macros) symbols global , have default visibility. way can think of achieve same have _foo
functions private visibility , have corresponding foo
functions default visibility , calling "hidden" counterparts.
a better way?
that, however, requires chunk of code changed. therefore prefer not go there unless there no other way.
so closes os x alternative or easiest way same semantics/behavior?
on os x, calls made within library automatically direct calls , not go through dyld stub. evidence fact if want able inject alternative functions service call, you'll need use interposable force indirect access symbols , force execution of call through dyld stubs. otherwise, default, local calls direct , not incur overhead of running through dyld.
thus, optimization on linux default behavior , alias not needed.
still, if want make platform compatible code simpler, can still make aliases. need use "weak_import" or "weak" (if want coalesced) attribute name.
extern typeof (_name) name __attribute(weak_import, alias("_name"));
apple reference on weak linking: marking symbols weak linking
apple reference on mach-o runtime binding : scope , treatment of symbol definitions
Comments
Post a Comment