<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="">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></body></html>