Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to build libunwind for wasm? #72771

Closed
trcrsired opened this issue Nov 19, 2023 · 9 comments · Fixed by #92192
Closed

How to build libunwind for wasm? #72771

trcrsired opened this issue Nov 19, 2023 · 9 comments · Fixed by #92192

Comments

@trcrsired
Copy link

trcrsired commented Nov 19, 2023

error: "Unsupported architecture."

FAILED: src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj
/home/cqwrteur/llvm_toolchains/x86_64-pc-linux-gnu/bin/clang++ --target=wasm32-wasi --sysroot=/home/cqwrteur/toolchains/sysroots/wasm32-wasi/wasm32-wasi -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBUNWIND_IS_BAREMETAL -I/home/cqwrteur/projects/llvm-project/libunwind/include -Wno-array-bounds -Wno-cast-function-type -Wno-uninitialized -fuse-ld=lld -Wno-misleading-indentation -Wno-global-constructors -Wno-unused-command-line-argument -O3 -DNDEBUG -std=c++11 -Werror=return-type -funwind-tables -nostdinc++ -D_DEBUG -UNDEBUG -D_LIBUNWIND_IS_NATIVE_ONLY -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -pedantic -fno-rtti  -fstrict-aliasing -fno-exceptions -fno-rtti -MD -MT src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj -MF src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj.d -o src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj -c /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:12:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/include/libunwind.h:16:
/home/cqwrteur/projects/llvm-project/libunwind/include/__libunwind_config.h:184:4: error: "Unsupported architecture."
  184 | #  error "Unsupported architecture."
      |    ^
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:12:
/home/cqwrteur/projects/llvm-project/libunwind/include/libunwind.h:71:17: error: use of undeclared identifier '_LIBUNWIND_CONTEXT_SIZE'
   71 |   uint64_t data[_LIBUNWIND_CONTEXT_SIZE];
      |                 ^
/home/cqwrteur/projects/llvm-project/libunwind/include/libunwind.h:76:17: error: use of undeclared identifier '_LIBUNWIND_CURSOR_SIZE'
   76 |   uint64_t data[_LIBUNWIND_CURSOR_SIZE];
      |                 ^
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:30:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/AddressSpace.hpp:23:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/EHHeaderParser.hpp:17:
/home/cqwrteur/projects/llvm-project/libunwind/src/DwarfParser.hpp:69:26: error: use of undeclared identifier '_LIBUNWIND_HIGHEST_DWARF_REGISTER'
   69 |     kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
      |                          ^
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:31:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/UnwindCursor.hpp:50:
/home/cqwrteur/projects/llvm-project/libunwind/src/RWMutex.hpp:19:10: fatal error: 'pthread.h' file not found
   19 | #include <pthread.h>
      |          ^~~~~~~~~~~
5 errors generated.
ninja: build stopped: subcommand failed.
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 19, 2023

@llvm/issue-subscribers-backend-webassembly

Author: cqwrteur (trcrsired)

error: "Unsupported architecture."
FAILED: src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj
/home/cqwrteur/llvm_toolchains/x86_64-pc-linux-gnu/bin/clang++ --target=wasm32-wasi --sysroot=/home/cqwrteur/toolchains/sysroots/wasm32-wasi/wasm32-wasi -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBUNWIND_IS_BAREMETAL -I/home/cqwrteur/projects/llvm-project/libunwind/include -Wno-array-bounds -Wno-cast-function-type -Wno-uninitialized -fuse-ld=lld -Wno-misleading-indentation -Wno-global-constructors -Wno-unused-command-line-argument -O3 -DNDEBUG -std=c++11 -Werror=return-type -funwind-tables -nostdinc++ -D_DEBUG -UNDEBUG -D_LIBUNWIND_IS_NATIVE_ONLY -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -pedantic -fno-rtti  -fstrict-aliasing -fno-exceptions -fno-rtti -MD -MT src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj -MF src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj.d -o src/CMakeFiles/unwind_static_objects.dir/libunwind.cpp.obj -c /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:12:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/include/libunwind.h:16:
/home/cqwrteur/projects/llvm-project/libunwind/include/__libunwind_config.h:184:4: error: "Unsupported architecture."
  184 | #  error "Unsupported architecture."
      |    ^
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:12:
/home/cqwrteur/projects/llvm-project/libunwind/include/libunwind.h:71:17: error: use of undeclared identifier '_LIBUNWIND_CONTEXT_SIZE'
   71 |   uint64_t data[_LIBUNWIND_CONTEXT_SIZE];
      |                 ^
/home/cqwrteur/projects/llvm-project/libunwind/include/libunwind.h:76:17: error: use of undeclared identifier '_LIBUNWIND_CURSOR_SIZE'
   76 |   uint64_t data[_LIBUNWIND_CURSOR_SIZE];
      |                 ^
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:30:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/AddressSpace.hpp:23:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/EHHeaderParser.hpp:17:
/home/cqwrteur/projects/llvm-project/libunwind/src/DwarfParser.hpp:69:26: error: use of undeclared identifier '_LIBUNWIND_HIGHEST_DWARF_REGISTER'
   69 |     kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
      |                          ^
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/libunwind.cpp:31:
In file included from /home/cqwrteur/projects/llvm-project/libunwind/src/UnwindCursor.hpp:50:
/home/cqwrteur/projects/llvm-project/libunwind/src/RWMutex.hpp:19:10: fatal error: 'pthread.h' file not found
   19 | #include &lt;pthread.h&gt;
      |          ^~~~~~~~~~~
5 errors generated.
ninja: build stopped: subcommand failed.
</details>

@dschuff
Copy link
Member

dschuff commented Nov 21, 2023

I don't think libunwind can be built for wasm, at least not in its "normal" form. IIUC it depends on being able to directly save and restore the values of the stack pointer, and that's just not possible on the wasm architecture (the underlying stack and return addresses are hidden from the wasm code).
If you wanted to build some unwinding functionality it would need to be on top of the wasm exception handling support builtin to the architecture. As an example of how you might do this, you can take a look at the implementation in libcxxabi (e6cbba7). libcxxabi is built on top of the libunwind API on most architectures, but on wasm it uses the EH support in the VM.

@aheejin
Copy link
Member

aheejin commented Nov 23, 2023

We only use libunwind's API structure and not really the implementation part like libunwind.cpp; it deals with the lower level stuff than wasm user library can handle, as @dschuff said. Effectively the only file we use is https://github.com/llvm/llvm-project/blob/main/libunwind/src/Unwind-wasm.c, which consists of some interface functions that interact with libc++abi.

To prevent this kind of confusion from happening, I guess it would be better to consider wrapping libunwind.cpp with #ifndef __USING_WASM_EXCEPTION__. I'll submit a PR for that.

@aheejin
Copy link
Member

aheejin commented Nov 23, 2023

#73196

@trcrsired
Copy link
Author

_LIBUNWIND_CONTEXT_SIZE

This macro is not defined. You are not clear on how to use this tbh

@arjunr2
Copy link

arjunr2 commented May 13, 2024

It looks like the issue isn't resolved yet -- https://github.com/llvm/llvm-project/blob/main/libunwind/include/__libunwind_config.h is still included in the build for libunwind.cpp here: https://github.com/llvm/llvm-project/blob/main/libunwind/src/libunwind.cpp#L12, throwing the same error as posed in the question.

Is the intention to remove libunwind.cpp completely from the build?

@aheejin
Copy link
Member

aheejin commented May 14, 2024

Can you try #92192 and let me know if that works?

Is the intention to remove libunwind.cpp completely from the build?

We don't need that file very much actually, but it contains some common debug-printing routines, so we decided to include it while excluding the bulk of the non-debugging contents. Now I see why that caused compilation problems.

@arjunr2
Copy link

arjunr2 commented May 15, 2024

It works now, but the CMake still pulls in the assembly file UnwindRegistersSave.S and UnwindRegistersRestore.S. It would probably help to add a CMake option to disable including them in the library or stub them out with the wasm define

@aheejin
Copy link
Member

aheejin commented May 15, 2024

Excluded the .S files too.

aheejin added a commit that referenced this issue May 21, 2024
This tries to make Wasm compilable in LLVM tree with CMake for
non-Emscripten platform.

This
- Adds `-D__USING_WASM_EXCEPTIONS__` when you compile with
`-fwasm-exceptions` (like other EH options) in Clang
- Exclude `UnwindLevel1.c`, `UnwindRegistersSave.S`, and
`UnwindRegistersRestore.S` when compiling with Wasm
- Changed some `__USING_WASM_EXCEPTIONS__` to `__wasm__`; they should be
applied when compiling with Wasm w/o exceptions.
- Define some unused macros to make it compile

Fixes #72771.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants