Linux OS weak character aliases on OS X, or the closest equivalent?

What am I doing

When writing shared libraries for Linux, I try to pay attention to movement, visibility of characters, GOT / PLT, etc.

When applicable, I try to avoid calling PLT stubs when functions from the same library call each other. For example, suppose a shared object provides two public functions - foo() and bar() (either of which can be called by the user). However, the bar() function also calls foo() . So what I do in this case is the following:

  • Define the _foo() and _bar() functions that have confidential visibility.
  • Define the weak aliases foo() and bar() for _foo() and _bar() respectively.

Thus, code in a shared object never uses weak characters. It calls directly local functions. For example, when _bar() is called, it calls _foo() directly.

But users are unaware of the _* functions and always use the corresponding weak aliases.

How i do it

On Linux, this is achieved using the following construct:

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

Problem

Unfortunately, this does not work for OS X. I do not have deep knowledge about OS X or its binary formats, so I thought a little and found some examples of weak functions (for example this one ), but this is not quite the same as you can have a weak character, but not a weak character, which is an alias for the local DSO function.

Possible Solution...

At the moment, I have disabled this feature (which is implemented using macros), so that all characters are global and have default visibility. The only way I can come now is to have all _foo functions with private visibility and have the corresponding foo functions with default visibility and call their β€œhidden” copies.

The best way?

This, however, requires a good piece of code that needs to be changed. Therefore, I would prefer not to go there if there is no other way.

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

+59
c linux linker dynamic-linking macos
Aug 15 '13 at 19:27
source share
1 answer

In OS X, calls made to the library are automatically direct calls and do not go through the dyld stub. Evidence of this is that if you want to be able to introduce alternative functions to service the call, you need to use interposable to force indirect access to characters and force the call through dyld stubs. Otherwise, by default, local calls will be direct and will not bear the overhead of starting through dyld.

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

However, if you want to do this simply to make your platform-compatible code simpler, you can still make aliases. You just need to use weak_import or weak (if you want to combine) as the name of your attribute.

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

Apple link for weak connection: Character labeling for weak connection
Apple Mach-O Runtime Link: Scope and Character Definition Processing

+1
Jun 13 '14 at 23:13
source share



All Articles