Export the unwinder implementation from libc.

This supports the soong commit which causes most platform binaries to stop
statically linking against the unwinder implementation. The soong commit
message has more motivation for this change.

ARM32 uses LLVM libunwind, while all other platforms use libgcc as the
unwinder implementation. This matches the current choices of unwinders on
the various architectures, but means that apps which were directly linking
against the libc.so unwinder symbols on ARM32 are now using LLVM libunwind
instead of libgcc.

Set libc_headers sdk_version to 1 so that libunwind_llvm can depend on it,
and stop statically linking libunwind into libc_malloc_debug.

Bug: 144430859
Change-Id: I52c7f7893d93f500383aeb0b76086c3b6f1935a5
diff --git a/libc/Android.bp b/libc/Android.bp
index 26cd3a3..2a5bce7 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1711,7 +1711,7 @@
                 keep_symbols_and_debug_frame: true,
             },
 
-            whole_static_libs: [ "libgcc_stripped" ],
+            whole_static_libs: [ "libunwind_llvm" ],
         },
         arm64: {
             version_script: ":libc.arm64.map",
@@ -1721,6 +1721,8 @@
             strip: {
                 keep_symbols: true,
             },
+
+            whole_static_libs: [ "libgcc_stripped" ],
         },
         x86: {
             // TODO: This is to work around b/24465209. Remove after root cause is fixed.
@@ -1734,6 +1736,8 @@
             strip: {
                 keep_symbols: true,
             },
+
+            whole_static_libs: [ "libgcc_stripped" ],
         },
         x86_64: {
             version_script: ":libc.x86_64.map",
@@ -1743,6 +1747,8 @@
             strip: {
                 keep_symbols: true,
             },
+
+            whole_static_libs: [ "libgcc_stripped" ],
         },
     },
 
@@ -1827,6 +1833,14 @@
     stl: "none",
     system_shared_libs: [],
 
+    // The build system generally requires that any dependencies of a target
+    // with an sdk_version must have a lower sdk_version. By setting sdk_version
+    // to 1 we let targets with an sdk_version that need to depend on the libc
+    // headers but cannot depend on libc itself due to circular dependencies
+    // (such as libunwind_llvm) depend on the headers. Setting sdk_version to 1
+    // is correct because the headers can support any sdk_version.
+    sdk_version: "1",
+
     export_include_dirs: [
         "include",
         "kernel/uapi",
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 01c9b0c..56877cd 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1517,6 +1517,36 @@
     tss_delete;
     tss_get;
     tss_set;
+
+    # Unwinder implementation
+    __aeabi_unwind_cpp_pr0; # apex llndk arm
+    __aeabi_unwind_cpp_pr1; # apex llndk arm
+    __aeabi_unwind_cpp_pr2; # apex llndk arm
+    __deregister_frame; # apex llndk arm64 x86 x86_64
+    __gnu_unwind_frame; # apex llndk arm
+    __register_frame; # apex llndk arm64 x86 x86_64
+    _Unwind_Backtrace; # apex llndk
+    _Unwind_Complete; # apex llndk arm
+    _Unwind_DeleteException; # apex llndk
+    _Unwind_Find_FDE; # apex llndk
+    _Unwind_FindEnclosingFunction; # apex llndk
+    _Unwind_ForcedUnwind; # apex llndk arm64 x86 x86_64
+    _Unwind_GetCFA; # apex llndk
+    _Unwind_GetDataRelBase; # apex llndk
+    _Unwind_GetGR; # apex llndk
+    _Unwind_GetIP; # apex llndk
+    _Unwind_GetIPInfo; # apex llndk
+    _Unwind_GetLanguageSpecificData; # apex llndk
+    _Unwind_GetRegionStart; # apex llndk
+    _Unwind_GetTextRelBase; # apex llndk
+    _Unwind_RaiseException; # apex llndk
+    _Unwind_Resume; # apex llndk
+    _Unwind_Resume_or_Rethrow; # apex llndk
+    _Unwind_SetGR; # apex llndk
+    _Unwind_SetIP; # apex llndk
+    _Unwind_VRS_Get; # apex llndk arm
+    _Unwind_VRS_Pop; # apex llndk arm
+    _Unwind_VRS_Set; # apex llndk arm
 } LIBC_Q;
 
 LIBC_PRIVATE {
@@ -1591,9 +1621,6 @@
     __aeabi_ul2d; # arm
     __aeabi_ul2f; # arm
     __aeabi_uldivmod; # arm
-    __aeabi_unwind_cpp_pr0; # arm
-    __aeabi_unwind_cpp_pr1; # arm
-    __aeabi_unwind_cpp_pr2; # arm
     __arm_fadvise64_64; # arm
     __ashldi3; # arm
     __ashrdi3; # arm
@@ -1631,7 +1658,6 @@
     __gnu_ldivmod_helper; # arm
     __gnu_uldivmod_helper; # arm
     __gnu_Unwind_Find_exidx; # arm
-    __gnu_unwind_frame; # arm
     __gtdf2; # arm
     __gtsf2; # arm
     __ledf2; # arm
@@ -1676,18 +1702,6 @@
     __unordsf2; # arm
     __wait4; # arm x86 mips
     _fwalk; # arm x86 mips
-    _Unwind_Backtrace; # arm
-    _Unwind_Complete; # arm
-    _Unwind_DeleteException; # arm
-    _Unwind_GetDataRelBase; # arm
-    _Unwind_GetLanguageSpecificData; # arm
-    _Unwind_GetRegionStart; # arm
-    _Unwind_GetTextRelBase; # arm
-    _Unwind_RaiseException; # arm
-    _Unwind_Resume; # arm
-    _Unwind_Resume_or_Rethrow; # arm
-    _Unwind_VRS_Get; # arm
-    _Unwind_VRS_Set; # arm
     android_getaddrinfofornetcontext;
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
diff --git a/libc/malloc_debug/Android.bp b/libc/malloc_debug/Android.bp
index 4256cc4..b04cfe1 100644
--- a/libc/malloc_debug/Android.bp
+++ b/libc/malloc_debug/Android.bp
@@ -65,14 +65,6 @@
 
     stl: "libc++_static",
 
-    // Only need this for arm since libc++ uses its own unwind code that
-    // doesn't mix with the other default unwind code.
-    arch: {
-        arm: {
-            static_libs: ["libunwind_llvm"],
-        },
-    },
-
     static_libs: [
         "libasync_safe",
         "libbase",