{"id":58,"date":"2016-07-24T20:46:12","date_gmt":"2016-07-24T12:46:12","guid":{"rendered":"http:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/?p=58"},"modified":"2017-03-08T03:32:21","modified_gmt":"2017-03-07T19:32:21","slug":"a-new-gentoo-stage4-musl-clang","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/2016\/07\/24\/a-new-gentoo-stage4-musl-clang\/","title":{"rendered":"A new Gentoo stage4: musl + clang"},"content":{"rendered":"<p>I&#8217;m glad to announce that I just successfully deployed the GNU-free toolchain, which I&#8217;ve been working on so far, into a musl-based Gentoo stage4. Everything in this stage4, except for the kernel, is built by clang with\u00a0non-GNU runtime libs. I&#8217;m now using this as my main Linux system, and luckily nothing breaks so far \ud83d\ude42<\/p>\n<p>To be more specific, the runtime environments of this stage4 is composed of the following components:<\/p>\n<ul>\n<li>C runtime: musl<\/li>\n<li>C++ runtime\n<ul>\n<li>C++ stdlib: libc++<\/li>\n<li>C++ ABI lib: libcxxrt [1]<\/li>\n<li>stack unwinding lib: libunwind<\/li>\n<\/ul>\n<\/li>\n<li>compiler: clang<\/li>\n<\/ul>\n<p>Let&#8217;s take program \/usr\/bin\/ld.gold for example, which is written in C++, and see its runtime dependencies:<\/p>\n<pre><code>$ ldd \/usr\/bin\/ld.gold\r\n\t\/lib\/ld-musl-x86_64.so.1 (0x559f8b598000)\r\n\tlibz.so.1 =&gt; \/lib\/libz.so.1 (0x7f5a5b7a9000)\r\n\tlibc++.so.1 =&gt; \/usr\/lib\/libc++.so.1 (0x7f5a5b6ea000)\r\n\tlibcxxrt.so.1 =&gt; \/usr\/lib\/libcxxrt.so.1 (0x7f5a5b4cd000)\r\n\tlibunwind.so.1 =&gt; \/usr\/lib\/libunwind.so.1 (0x7f5a5b4c4000)\r\n\tlibc.so =&gt; \/lib\/ld-musl-x86_64.so.1 (0x559f8b598000)\r\n<\/code><\/pre>\n<p>Apparently none of\u00a0the above libs is related to gcc. So this program is just free from gcc!<\/p>\n<p>Actually I&#8217;ve already figured out how to glue clang and those non-GNU C++ runtime libs together weeks ago; but this is the first time I put it into serious use. The most challenging part is to rebuild @world with this toolchain; I&#8217;m really nervous that\u00a0countless packages get broken after I issue the command\u00a0<code>emerge -e @world<\/code>. Surprisingly enough, only two packages are broken.<\/p>\n<p>The first broken package\u00a0is\u00a0iproute2, which depends on\u00a0libmnl and libmnl is mis-compiled by clang. libmnl has an unpleasant past with clang [2], but its new\u00a0versions are fine. So the fix is simple: just upgrade libmnl to the latest version available (use ~amd64).<\/p>\n<p>The second broken package, ironically, is clang\/llvm itself. It turned out the libunwind I&#8217;m using lacks a few functions essential to llvm. Actually there&#8217;re two versions of libunwind available: one is from nongnu [3] which already exists in Gentoo&#8217;s repo for a while; the other one is developed by\u00a0LLVM and is\u00a0<em>almost\u00a0<\/em>functionally equivalent to the former. Initially I just used the nongnu version, since there&#8217;s no package for LLVM&#8217;s libunwind yet. Unfortunately, the nongnu one works fine with everything else except for llvm itself. I just had to stop being lazy and wrote an ebuild for LLVM&#8217;s libunwind. Luckily again, it has everything we need, including those functions missing in the other libunwind.<\/p>\n<p>After that, while I&#8217;m using this new system, I encountered a few other broken packages. But they&#8217;re either incompatible with musl, or the gold linker (yes, it&#8217;s the \/usr\/bin\/ld.gold I just showed you). There&#8217;s nothing wrong with clang or the C++ runtime libs.<\/p>\n<p>I also found an interesting fact: the building of gcc involves several steps of bootstrapping and the final executable has no dependence on any external C++ runtime libs, though gcc itself written in C++. This means gcc still works even clang or the C++ runtime gets broken. So I don&#8217;t need\u00a0to worry about breaking packages since I can still rebuild them with gcc \ud83d\ude42<\/p>\n<p>Now that I have a working stage4, the next step is to make gcc-config support clang, so clang can act like a <strong>real<\/strong>\u00a0native compiler.<\/p>\n<p>Stay tuned!<\/p>\n<p>&nbsp;<\/p>\n<p>[1] libcxxrt is functionally equivalent to libc++abi, and is planned to be replaced by the latter later in this project<br \/>\n[2]\u00a0https:\/\/bugs.chromium.org\/p\/chromium\/issues\/detail?id=548786<br \/>\n[3]\u00a0http:\/\/www.nongnu.org\/libunwind\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m glad to announce that I just successfully deployed the GNU-free toolchain, which I&#8217;ve been working on so far, into a musl-based Gentoo stage4. Everything in this stage4, except for the kernel, is built by clang with\u00a0non-GNU runtime libs. I&#8217;m now using this as my main Linux system, and luckily nothing breaks so far \ud83d\ude42 &hellip; <a href=\"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/2016\/07\/24\/a-new-gentoo-stage4-musl-clang\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">A new Gentoo stage4: musl + clang<\/span><\/a><\/p>\n","protected":false},"author":161,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts\/58"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/users\/161"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":2,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":60,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts\/58\/revisions\/60"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}