[libc-commits] [llvm] [libunwind] [libc] [clang] [libunwind] Fix build for wasm (PR #79667)

via libc-commits libc-commits at lists.llvm.org
Fri Jan 26 19:40:41 PST 2024


https://github.com/trcrsired updated https://github.com/llvm/llvm-project/pull/79667

>From 1bd90e6a300dc8fd0b21a5a8dda2b40a41e6b128 Mon Sep 17 00:00:00 2001
From: trcrsired <uwgghhbcad at gmail.com>
Date: Fri, 26 Jan 2024 18:44:41 -0500
Subject: [PATCH 1/2] [libunwind] Fix build for wasm

The wasm unwind build appears to be dysfunctional, likely because the author has only supplied a customized LLVM build on request, rather than a fully functional patch.

This patch fixes the build

Apply formatting patch proposed by github bot

use "" to prevent CMAKE_SYSTEM_PROCESSOR not defined
---
 libunwind/include/__libunwind_config.h |  1 +
 libunwind/src/CMakeLists.txt           | 53 +++++++++++++++-----------
 libunwind/src/Unwind-wasm.c            | 16 ++++----
 libunwind/src/config.h                 | 15 ++++----
 4 files changed, 47 insertions(+), 38 deletions(-)

diff --git a/libunwind/include/__libunwind_config.h b/libunwind/include/__libunwind_config.h
index 8db336b2d727ce..1cda20d7225500 100644
--- a/libunwind/include/__libunwind_config.h
+++ b/libunwind/include/__libunwind_config.h
@@ -180,6 +180,7 @@
 #endif
 #define _LIBUNWIND_HIGHEST_DWARF_REGISTER                                      \
   _LIBUNWIND_HIGHEST_DWARF_REGISTER_LOONGARCH
+#elif defined(__wasm__)
 # else
 #  error "Unsupported architecture."
 # endif
diff --git a/libunwind/src/CMakeLists.txt b/libunwind/src/CMakeLists.txt
index 9c6f5d908b0945..b0838e2050f436 100644
--- a/libunwind/src/CMakeLists.txt
+++ b/libunwind/src/CMakeLists.txt
@@ -1,31 +1,38 @@
 # Get sources
 
-set(LIBUNWIND_CXX_SOURCES
-    libunwind.cpp
-    Unwind-EHABI.cpp
-    Unwind-seh.cpp
-    )
+if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32" OR
+  "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm64")
+  set(LIBUNWIND_C_SOURCES
+      Unwind-wasm.c
+      )
+else()
+  set(LIBUNWIND_CXX_SOURCES
+      libunwind.cpp
+      Unwind-EHABI.cpp
+      Unwind-seh.cpp
+      )
+
+  if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
+    list(APPEND LIBUNWIND_CXX_SOURCES
+      Unwind_AIXExtras.cpp
+      )
+  endif()
 
-if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
-  list(APPEND LIBUNWIND_CXX_SOURCES
-    Unwind_AIXExtras.cpp
-    )
-endif()
+  set(LIBUNWIND_C_SOURCES
+      UnwindLevel1.c
+      UnwindLevel1-gcc-ext.c
+      Unwind-sjlj.c
+      )
 
-set(LIBUNWIND_C_SOURCES
-    UnwindLevel1.c
-    UnwindLevel1-gcc-ext.c
-    Unwind-sjlj.c
-    Unwind-wasm.c
-    )
-set_source_files_properties(${LIBUNWIND_C_SOURCES}
-                            PROPERTIES
-                              COMPILE_FLAGS "-std=c99")
+  set(LIBUNWIND_ASM_SOURCES
+      UnwindRegistersRestore.S
+      UnwindRegistersSave.S
+      )
 
-set(LIBUNWIND_ASM_SOURCES
-    UnwindRegistersRestore.S
-    UnwindRegistersSave.S
-    )
+  set_source_files_properties(${LIBUNWIND_C_SOURCES}
+                              PROPERTIES
+                                COMPILE_FLAGS "-std=c99")
+endif()
 
 set(LIBUNWIND_HEADERS
     AddressSpace.hpp
diff --git a/libunwind/src/Unwind-wasm.c b/libunwind/src/Unwind-wasm.c
index f7f39d38b59c18..87be87e9fd92a8 100644
--- a/libunwind/src/Unwind-wasm.c
+++ b/libunwind/src/Unwind-wasm.c
@@ -10,14 +10,11 @@
 //
 //===----------------------------------------------------------------------===//
 
+#if __STDC_VERSION__ < 202311L
 #include <stdbool.h>
-
+#endif
 #include "config.h"
-
-#ifdef __USING_WASM_EXCEPTIONS__
-
 #include "unwind.h"
-#include <threads.h>
 
 _Unwind_Reason_Code __gxx_personality_wasm0(int version, _Unwind_Action actions,
                                             uint64_t exceptionClass,
@@ -35,7 +32,12 @@ struct _Unwind_LandingPadContext {
 
 // Communication channel between compiler-generated user code and personality
 // function
-thread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
+#if __STDC_VERSION__ >= 202311L
+thread_local
+#else
+_Thread_local
+#endif
+    struct _Unwind_LandingPadContext __wasm_lpad_context;
 
 /// Calls to this function is in landing pads in compiler-generated user code.
 /// In other EH schemes, stack unwinding is done by libunwind library, which
@@ -119,5 +121,3 @@ _LIBUNWIND_EXPORT uintptr_t
 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
   return 0;
 }
-
-#endif // defined(__USING_WASM_EXCEPTIONS__)
diff --git a/libunwind/src/config.h b/libunwind/src/config.h
index deb5a4d4d73d46..2a57df41acca9f 100644
--- a/libunwind/src/config.h
+++ b/libunwind/src/config.h
@@ -66,13 +66,14 @@
   #define _LIBUNWIND_EXPORT
   #define _LIBUNWIND_HIDDEN
 #else
-  #if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX)
-    #define _LIBUNWIND_EXPORT __declspec(dllexport)
-    #define _LIBUNWIND_HIDDEN
-  #else
-    #define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
-    #define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
-  #endif
+#if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX) &&               \
+    !defined(__wasm__)
+#define _LIBUNWIND_EXPORT __declspec(dllexport)
+#define _LIBUNWIND_HIDDEN
+#else
+#define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
+#define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
+#endif
 #endif
 
 #define STR(a) #a

>From f15c3e43a18d8eaa30e3d5b978b011d6798a1095 Mon Sep 17 00:00:00 2001
From: trcrsired <uwgghhbcad at gmail.com>
Date: Fri, 26 Jan 2024 22:35:23 -0500
Subject: [PATCH 2/2] [libunwind] logAPI functions should also be built

---
 libunwind/include/libunwind.h | 2 ++
 libunwind/src/CMakeLists.txt  | 3 +++
 libunwind/src/libunwind.cpp   | 2 ++
 3 files changed, 7 insertions(+)

diff --git a/libunwind/include/libunwind.h b/libunwind/include/libunwind.h
index b2dae8feed9a3b..63e147ae2423e2 100644
--- a/libunwind/include/libunwind.h
+++ b/libunwind/include/libunwind.h
@@ -15,6 +15,7 @@
 
 #include <__libunwind_config.h>
 
+#ifndef __wasm__
 #include <stdint.h>
 #include <stddef.h>
 
@@ -1299,5 +1300,6 @@ enum {
   UNW_LOONGARCH_F30 = 62,
   UNW_LOONGARCH_F31 = 63,
 };
+#endif
 
 #endif
diff --git a/libunwind/src/CMakeLists.txt b/libunwind/src/CMakeLists.txt
index b0838e2050f436..2b21f4400797ac 100644
--- a/libunwind/src/CMakeLists.txt
+++ b/libunwind/src/CMakeLists.txt
@@ -5,6 +5,9 @@ if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32" OR
   set(LIBUNWIND_C_SOURCES
       Unwind-wasm.c
       )
+  set(LIBUNWIND_CXX_SOURCES
+      libunwind.cpp
+     )
 else()
   set(LIBUNWIND_CXX_SOURCES
       libunwind.cpp
diff --git a/libunwind/src/libunwind.cpp b/libunwind/src/libunwind.cpp
index 217dde90986379..cd12570da1141a 100644
--- a/libunwind/src/libunwind.cpp
+++ b/libunwind/src/libunwind.cpp
@@ -12,6 +12,7 @@
 #include <libunwind.h>
 
 #include "config.h"
+#ifndef __wasm__
 #include "libunwind_ext.h"
 
 #include <stdlib.h>
@@ -431,6 +432,7 @@ int __unw_remove_find_dynamic_unwind_sections(
 }
 
 #endif // __APPLE__
+#endif
 
 // Add logging hooks in Debug builds only
 #ifndef NDEBUG



More information about the libc-commits mailing list