[cfe-dev] Build libc++ against musl-libc.

邓尧 torshie at gmail.com
Mon Sep 16 23:25:56 PDT 2013


Hi,
I'm trying to build libc++ against musl-libc ( www.musl-libc.org ), another
linux C library. I have successfully built a musl-libc based C toolchain, a
freestanding C++ compiler, and some C++ runtime libraries like libcxxrt,
libunwind, I even succeeded in building libc++ against musl-libc, but a
simple hello-world program won't run.

the following code would throw a bad_cast exception:

#include <locale>
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    locale loc("C");
    //cout.imbue(loc);  // If this line is added, the whole program runs
well.
    cout << "hello" << endl;
    return 0;
}

This is the call stack of the thrown exception:
#6  0x00000000004ce2d9 in __cxa_throw ()
#7  0x0000000000422a40 in std::__1::locale::__imp::use_facet(long) const ()
    at /home/hoolala/workspace/bootcxx/out/libcxx/src/locale.cpp:439
#8  0x0000000000423379 in
std::__1::locale::use_facet(std::__1::locale::id&) const ()
    at /home/hoolala/workspace/bootcxx/out/libcxx/src/locale.cpp:581
#9  0x00000000004005ad in std::__1::basic_ostream<char,
std::__1::char_traits<char> >& std::__1::operator<<
<std::__1::char_traits<char> >(std::__1::basic_ostream<char,
std::__1::char_traits<char> >&, char const*) ()
    at
/home/hoolala/workspace/bootcxx/out/sysroot/usr/include/c++/v1/__locale:174
#10 0x000000000040035a in main () at loc.cpp:12

I applied a simple patch to the original libc++ code:

Index: include/__locale
===================================================================
--- include/__locale    (revision 190843)
+++ include/__locale    (working copy)
@@ -376,7 +376,7 @@
     static const mask xdigit = _ISXDIGIT;
     static const mask blank  = _ISBLANK;
 #else  // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ || EMSCRIPTEN ||
__sun__
-    typedef unsigned long mask;
+    typedef unsigned short mask;
     static const mask space  = 1<<0;
     static const mask print  = 1<<1;
     static const mask cntrl  = 1<<2;
Index: include/locale
===================================================================
--- include/locale    (revision 190843)
+++ include/locale    (working copy)
@@ -829,7 +829,7 @@
         typename remove_reference<decltype(errno)>::type __save_errno =
errno;
         errno = 0;
         char *__p2;
-        long long __ll = strtoll_l(__a, &__p2, __base,
_LIBCPP_GET_C_LOCALE);
+        long long __ll = strtoll(__a, &__p2, __base);
         typename remove_reference<decltype(errno)>::type __current_errno =
errno;
         if (__current_errno == 0)
             errno = __save_errno;
@@ -869,7 +869,7 @@
         typename remove_reference<decltype(errno)>::type __save_errno =
errno;
         errno = 0;
         char *__p2;
-        unsigned long long __ll = strtoull_l(__a, &__p2, __base,
_LIBCPP_GET_C_LOCALE);
+        unsigned long long __ll = strtoull(__a, &__p2, __base);
         typename remove_reference<decltype(errno)>::type __current_errno =
errno;
         if (__current_errno == 0)
             errno = __save_errno;
@@ -899,7 +899,7 @@
         typename remove_reference<decltype(errno)>::type __save_errno =
errno;
         errno = 0;
         char *__p2;
-        long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
+        long double __ld = strtold(__a, &__p2);
         typename remove_reference<decltype(errno)>::type __current_errno =
errno;
         if (__current_errno == 0)
             errno = __save_errno;
Index: src/locale.cpp
===================================================================
--- src/locale.cpp    (revision 190843)
+++ src/locale.cpp    (working copy)
@@ -1005,6 +1005,8 @@
 extern "C" const int ** __ctype_toupper_loc();
 #endif

+extern "C" const unsigned short ** __ctype_b_loc();
+
 const ctype<char>::mask*
 ctype<char>::classic_table()  _NOEXCEPT
 {
@@ -1027,10 +1029,11 @@
 #else
     // Platform not supported: abort so the person doing the port knows
what to
     // fix
-# warning  ctype<char>::classic_table() is not implemented
-    printf("ctype<char>::classic_table() is not implemented\n");
-    abort();
-    return NULL;
+//# warning  ctype<char>::classic_table() is not implemented
+    //printf("ctype<char>::classic_table() is not implemented\n");
+    //abort();
+    //return NULL;
+    return *__ctype_b_loc();
 #endif
 }

Did I miss anything in the patch ?

Thanks
Yao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130917/09072c2a/attachment.html>


More information about the cfe-dev mailing list