<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Sorry for the noise…</div><div class=""><br class=""></div><div class="">The previous version generated a non-external symbol and didn’t work between modules. It needs .globl to be exported.</div><div class=""><br class=""></div><div class="">I don’t think = is generating N_INDR. In any case I have source compat, just not weak indirect aliases.</div><div class=""><br class=""></div><div class="">This version works between modules.</div><div class=""><br class=""></div><div class="">#define weak_alias(old, new) \<br class="">        __asm__(".globl _" #new); \<br class="">        __asm__("_" #new " = _" #old); \<br class="">        extern __typeof(old) new __attribute__((weak_import))<br class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 8 Jun 2017, at 3:30 PM, Michael Clark <<a href="mailto:michaeljclark@mac.com" class="">michaeljclark@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I think I have weak indirect aliases working on Mach-O. Can anyone verify this?</div><div class=""><br class=""></div><div class="">Note, the .set nor .weak_ref create an indirect alias, but after reading Tim’s patch, I changed the macro to use = and now the symbol has a lowercase t in nm output and .globl was no longer required to get it to appear in the symbol table.</div><br class="">/*<br class="">        $ cc -c alias.c -o alias.o<br class="">        $ cc alias.o<br class="">        $ ./a.out <br class="">        foo<br class="">        $ nm alias.o<br class="">        0000000000000000 T ___foo<br class="">        0000000000000000 t _foo<br class="">        0000000000000020 T _main<br class="">                         U _printf<br class="">*/<br class=""><br class="">#include <stdio.h><br class=""><br class="">#if HAVE_ALIASES<br class=""><br class="">#define weak_alias(old, new) \<br class="">        extern __typeof(old) new __attribute__((weak, alias(#old)))<br class=""><br class="">#else<br class=""><br class="">#define weak_alias(old, new) \<br class="">        __asm__("_" #new " = _" #old); \<br class="">        extern __typeof(old) new __attribute__((weak_import))<br class=""><br class="">#endif<br class=""><br class="">void __foo()<br class="">{<br class="">        printf("foo\n");<br class="">}<br class=""><br class="">weak_alias(__foo, foo);<br class=""><br class="">int main()<br class="">{<br class="">        foo();<br class="">}<div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 8 Jun 2017, at 1:22 PM, Michael Clark <<a href="mailto:michaeljclark@mac.com" class="">michaeljclark@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I believe I have solved the problem at least such that I can maintain source compatibility with musl’s use of a weak_alias macro and calls through the aliases. Thanks to Jameson who gave me some off-list advice. I would like to post this to the mailing list so that others who are trying to get weak aliases working on Mach-O might find this in Google versus what I found which was error messages and no apparent working solutions.</div><div class=""><br class=""></div><div class="">From the nm output, it seems the approach below is generating a strong direct alias, however in my case that is satisfactory as I don’t need interposition support, rather my goal is source compatibility. It seems weak_import has no effect when the symbol is defined in the same module. i.e. it does not create weak linkage, and if I don’t define the symbol with .set I get unresolved symbol on link.</div><div class=""><br class=""></div><div class="">It seems we need N_INDR support as per the blog post and in this commit, and some syntax to coax the assembler into making a weak indirect alias:</div><div class=""><br class=""></div><div class="">- <a href="http://blog.omega-prime.co.uk/?p=121" class="">http://blog.omega-prime.co.uk/?p=121</a></div><div class="">- <a href="http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20140804/230120.html" class="">http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20140804/230120.html</a></div><div class=""><br class=""></div><div class="">Here is my strong direct alias, versus ideally what should be a weak indirect alias. It works for my use case (source compat).</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">$ cat alias.c </div><div class="">#include <stdio.h></div><div class=""><br class=""></div><div class="">#if HAVE_ALIASES</div><div class=""><br class=""></div><div class="">#define weak_alias(old, new) \</div><div class="">        extern __typeof(old) new __attribute__((weak, alias(#old)))</div><div class=""><br class=""></div><div class="">#else</div><div class=""><br class=""></div><div class="">#define weak_alias(old, new) \</div><div class=""><span class="Apple-tab-span" style="white-space:pre">       </span>__asm__(".globl _" #new); \</div><div class=""><span class="Apple-tab-span" style="white-space:pre">     </span>__asm__(".set _" #new ", _" #old); \</div><div class=""><span class="Apple-tab-span" style="white-space:pre">  </span>extern __typeof(old) new __attribute__((weak_import))</div><div class=""><br class=""></div><div class="">#endif</div><div class=""><br class=""></div><div class="">void __foo()</div><div class="">{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">      </span>printf("foo\n");</div><div class="">}</div><div class=""><br class=""></div><div class="">weak_alias(__foo, foo);</div><div class=""><br class=""></div><div class="">int main()</div><div class="">{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">       </span>foo();</div><div class="">}</div><div class="">$ cc -DHAVE_ALIASES=1 -c alias.c -o alias.o</div><div class="">alias.c:22:1: error: only weak aliases are supported on darwin</div><div class="">weak_alias(__foo, foo);</div><div class="">^</div><div class="">alias.c:6:55: note: expanded from macro 'weak_alias'</div><div class="">        extern __typeof(old) new __attribute__((weak, alias(#old)))</div><div class="">                                                      ^</div><div class="">1 error generated.</div><div class="">$ cc -c alias.c -o alias.o</div><div class="">$ nm -x alias.o </div><div class="">0000000000000000 0f 01 0000 00000001 ___foo</div><div class="">0000000000000000 0f 01 0000 00000003 _foo</div><div class="">0000000000000020 0f 01 0000 00000008 _main</div><div class="">0000000000000000 01 00 0000 0000000e _printf</div><div class="">$ cc alias.o</div><div class="">$ ./a.out </div><div class="">foo</div></blockquote><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 7 Jun 2017, at 11:08 AM, Michael Clark <<a href="mailto:michaeljclark@mac.com" class="">michaeljclark@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi Folks,</div><div class=""><br class=""></div><div class="">I’m working on a port of musl libc to macos (arch triple is “x86_64-xnu-musl”) to solve some irreconcilable issues I’m having with libSystem.dylib. I don’t want to use glibc for various reasons, mainly because I want to static link. I have static PIE + ASLR working which is not actually supported by the Apple toolchain (*1), but I managed to get it to work. I’m sure Apple might say “Don’t do that”, but from looking at the history of the xnu kernel ABI, it seems to be very stable between versions.</div><div class=""><br class=""></div><div class="">In any case the musl libc source makes extensive use of weak aliases, perhaps to allow easier interposition of C library routines, however aliases, weak or otherwise are not currently supported by ld64.</div><div class=""><br class=""></div><div class="">It appears that the mach-o format supports aliases, but the functionality has not been exposed via the linker (ld64/LLD).</div><div class=""><br class=""></div><div class="">- <a href="http://blog.omega-prime.co.uk/?p=121" class="">http://blog.omega-prime.co.uk/?p=121</a></div><div class=""><br class=""></div><div class="">The musl code does the following which currently errors out saying aliases are not currently supported:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">#undef weak_alias</div><div class="">#define weak_alias(old, new) \</div><div class="">        extern __typeof(old) new __attribute__((weak, alias(#old)))</div></blockquote><div class=""><br class=""></div><div class="">and the macro is used internally like this:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">int __pthread_join(pthread_t t, void **res)</div><div class="">{</div><div class="">        // implementation here</div><div class="">}</div><div class=""><br class=""></div><div class="">weak_alias(__pthread_join, pthread_join);</div></blockquote><div class=""><br class=""></div><div class="">The problem is the actual export used by clients is an alias and I want to maintain source compatibility.</div><div class=""><br class=""></div><div class="">I seem to have found a way to semi-emulate aliases (at least within one module). My goal is to at least turn them into strong aliases somehow, so I can at a minimum make the musl source compatible with clang on macos. The following compiles but foo is not exported:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">$ cat a.c</div><div class="">#include <stdio.h></div><div class=""><br class=""></div><div class="">void foo() __attribute__((weak_import)) __asm("_bar");</div><div class=""><br class=""></div><div class="">void bar()</div><div class="">{</div><div class="">        printf("bar\n");</div><div class="">}</div><div class=""><br class=""></div><div class="">int main()</div><div class="">{</div><div class="">        foo();</div><div class="">}</div><div class=""><br class=""></div><div class="">$ cc -c a.c -o a.o<br class="">$ nm a.o<br class="">0000000000000000 T _bar<br class="">0000000000000020 T _main<br class="">                 U _printf</div></blockquote><div class=""><br class=""></div><div class="">Any ideas how I can get foo as an exported symbol? </div><div class=""><br class=""></div><div class="">Is weak alias or plan alias support planned for mach-o in LLD?</div><div class=""><br class=""></div><div class="">The goal at a minimum is to make the weak_alias macro emit a strong alias with clang/ld64 or clang/LLD? so I don’t need to diverge too much from the upstream musl source (as the lack of alias support currently requires me to rename function declarations in the source). Of course pthreads which I’m working on now are going to be completely different… but musl has support for architecture specific overrides in its build system.</div><div class=""><br class=""></div><div class="">BTW I now have some quite non-trivial programs compiling against musl-xnu + libcxx + libcxxabi on macos.</div><div class=""><br class=""></div><div class="">There are a lot of libcxx changes like this:</div><div class=""><br class=""></div><div class="">-#ifdef __APPLE__<br class="">+#if defined(__APPLE__) && !defined(_LIBCPP_HAS_MUSL_LIBC)<br class=""><br class=""></div><div class="">Michael.</div><div class=""><br class=""></div><div class="">[1] <a href="https://gist.github.com/michaeljclark/0a805652ec4be987a782afb902f06a99" class="">https://gist.github.com/michaeljclark/0a805652ec4be987a782afb902f06a99</a></div></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></body></html>