Skip to content

Commit 119a427

Browse files
authored
Update libunwind to LLVM 19.1.4 (#22934)
Other than these two PRs I submitted to upstream LLVM, all other changes are unrelated upstream changes. llvm/llvm-project#92192 llvm/llvm-project#92840 This PR was created by running https://github.com/emscripten-core/emscripten/blob/main/system/lib/update_libunwind.py script.
1 parent f03605d commit 119a427

12 files changed

+120
-28
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.73 (in development)
2222
-----------------------
23+
- libunwind was updated to LLVM 19.1.4. (#22394)
2324

2425
3.1.72 - 11/19/24
2526
-----------------

system/lib/libunwind/include/__libunwind_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@
180180
#endif
181181
#define _LIBUNWIND_HIGHEST_DWARF_REGISTER \
182182
_LIBUNWIND_HIGHEST_DWARF_REGISTER_LOONGARCH
183+
#elif defined(__wasm__)
184+
// Unused
185+
#define _LIBUNWIND_CONTEXT_SIZE 0
186+
#define _LIBUNWIND_CURSOR_SIZE 0
183187
# else
184188
# error "Unsupported architecture."
185189
# endif

system/lib/libunwind/include/mach-o/compact_unwind_encoding.modulemap

Lines changed: 0 additions & 4 deletions
This file was deleted.

system/lib/libunwind/readme.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
llvm's libunwind
22
----------------
33

4-
These files are from the llvm-project based on release 17.0.4.
4+
These files are from the llvm-project based on release 19.1.4.
55

66
We maintain a local fork of llvm-project that contains any emscripten
77
specific patches:
88

99
https://github.com/emscripten-core/llvm-project
1010

11-
The current patch is based on the emscripten-libs-17 branch.
11+
The current patch is based on the emscripten-libs-19 branch.
1212

1313
Update Instructions
1414
-------------------
@@ -20,4 +20,4 @@ Modifications
2020

2121
For a list of changes from upstream see the libunwind files that are part of:
2222

23-
https://github.com/llvm/llvm-project/compare/llvmorg-17.0.4...emscripten-core:emscripten-libs-17
23+
https://github.com/llvm/llvm-project/compare/llvmorg-19.1.4...emscripten-core:emscripten-libs-19

system/lib/libunwind/src/Registers.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,13 @@ inline const char *Registers_ppc64::getRegisterName(int regNum) {
18151815
/// process.
18161816
class _LIBUNWIND_HIDDEN Registers_arm64;
18171817
extern "C" void __libunwind_Registers_arm64_jumpto(Registers_arm64 *);
1818+
1819+
#if defined(_LIBUNWIND_USE_GCS)
1820+
extern "C" void *__libunwind_cet_get_jump_target() {
1821+
return reinterpret_cast<void *>(&__libunwind_Registers_arm64_jumpto);
1822+
}
1823+
#endif
1824+
18181825
class _LIBUNWIND_HIDDEN Registers_arm64 {
18191826
public:
18201827
Registers_arm64();

system/lib/libunwind/src/UnwindCursor.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <errno.h>
3737
#include <signal.h>
3838
#include <sys/syscall.h>
39-
#include <sys/uio.h>
4039
#include <unistd.h>
4140
#define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1
4241
#endif
@@ -472,7 +471,7 @@ class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
472471
}
473472
#endif
474473

475-
#if defined(_LIBUNWIND_USE_CET)
474+
#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
476475
virtual void *get_registers() {
477476
_LIBUNWIND_ABORT("get_registers not implemented");
478477
}
@@ -955,7 +954,7 @@ class UnwindCursor : public AbstractUnwindCursor{
955954
virtual uintptr_t getDataRelBase();
956955
#endif
957956

958-
#if defined(_LIBUNWIND_USE_CET)
957+
#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
959958
virtual void *get_registers() { return &_registers; }
960959
#endif
961960

@@ -2416,7 +2415,7 @@ int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable,
24162415
}
24172416

24182417
// Reset LR in the current context.
2419-
newRegisters.setLR(NULL);
2418+
newRegisters.setLR(static_cast<uintptr_t>(NULL));
24202419

24212420
_LIBUNWIND_TRACE_UNWINDING(
24222421
"Extract info from lastStack=%p, returnAddress=%p",
@@ -2590,6 +2589,15 @@ void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
25902589
--pc;
25912590
#endif
25922591

2592+
#if !(defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)) && \
2593+
!defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
2594+
// In case of this is frame of signal handler, the IP saved in the signal
2595+
// handler points to first non-executed instruction, while FDE/CIE expects IP
2596+
// to be after the first non-executed instruction.
2597+
if (_isSignalFrame)
2598+
++pc;
2599+
#endif
2600+
25932601
// Ask address space object to find unwind sections for this pc.
25942602
UnwindInfoSections sects;
25952603
if (_addressSpace.findUnwindSections(pc, sects)) {
@@ -2997,7 +3005,7 @@ bool UnwindCursor<A, R>::isReadableAddr(const pint_t addr) const {
29973005
}
29983006
#endif
29993007

3000-
#if defined(_LIBUNWIND_USE_CET)
3008+
#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
30013009
extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
30023010
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
30033011
return co->get_registers();

system/lib/libunwind/src/UnwindLevel1.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#include "libunwind_ext.h"
3232
#include "unwind.h"
3333

34-
#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
34+
#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
35+
!defined(__wasm__)
3536

3637
#ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
3738

@@ -43,7 +44,7 @@
4344
// _LIBUNWIND_POP_CET_SSP is used to adjust CET shadow stack pointer and we
4445
// directly jump to __libunwind_Registers_x86/x86_64_jumpto instead of using
4546
// a regular function call to avoid pushing to CET shadow stack again.
46-
#if !defined(_LIBUNWIND_USE_CET)
47+
#if !defined(_LIBUNWIND_USE_CET) && !defined(_LIBUNWIND_USE_GCS)
4748
#define __unw_phase2_resume(cursor, fn) \
4849
do { \
4950
(void)fn; \
@@ -71,6 +72,19 @@
7172
__asm__ volatile("jmpq *%%rdx\n\t" :: "D"(cetRegContext), \
7273
"d"(cetJumpAddress)); \
7374
} while (0)
75+
#elif defined(_LIBUNWIND_TARGET_AARCH64)
76+
#define __cet_ss_step_size 8
77+
#define __unw_phase2_resume(cursor, fn) \
78+
do { \
79+
_LIBUNWIND_POP_CET_SSP((fn)); \
80+
void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
81+
void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
82+
__asm__ volatile("mov x0, %0\n\t" \
83+
"br %1\n\t" \
84+
: \
85+
: "r"(cetRegContext), "r"(cetJumpAddress) \
86+
: "x0"); \
87+
} while (0)
7488
#endif
7589

7690
static _Unwind_Reason_Code
@@ -169,6 +183,10 @@ unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
169183
}
170184
extern int __unw_step_stage2(unw_cursor_t *);
171185

186+
#if defined(_LIBUNWIND_USE_GCS)
187+
// Enable the GCS target feature to permit gcspop instructions to be used.
188+
__attribute__((target("gcs")))
189+
#endif
172190
static _Unwind_Reason_Code
173191
unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
174192
__unw_init_local(cursor, uc);
@@ -179,8 +197,12 @@ unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
179197
// uc is initialized by __unw_getcontext in the parent frame. The first stack
180198
// frame walked is unwind_phase2.
181199
unsigned framesWalked = 1;
182-
#ifdef _LIBUNWIND_USE_CET
200+
#if defined(_LIBUNWIND_USE_CET)
183201
unsigned long shadowStackTop = _get_ssp();
202+
#elif defined(_LIBUNWIND_USE_GCS)
203+
unsigned long shadowStackTop = 0;
204+
if (__chkfeat(_CHKFEAT_GCS))
205+
shadowStackTop = (unsigned long)__gcspr();
184206
#endif
185207
// Walk each frame until we reach where search phase said to stop.
186208
while (true) {
@@ -237,7 +259,7 @@ unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
237259
// against return address stored in CET shadow stack, if the 2 addresses don't
238260
// match, it means return address in normal stack has been corrupted, we return
239261
// _URC_FATAL_PHASE2_ERROR.
240-
#ifdef _LIBUNWIND_USE_CET
262+
#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
241263
if (shadowStackTop != 0) {
242264
unw_word_t retInNormalStack;
243265
__unw_get_reg(cursor, UNW_REG_IP, &retInNormalStack);
@@ -305,6 +327,10 @@ unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
305327
return _URC_FATAL_PHASE2_ERROR;
306328
}
307329

330+
#if defined(_LIBUNWIND_USE_GCS)
331+
// Enable the GCS target feature to permit gcspop instructions to be used.
332+
__attribute__((target("gcs")))
333+
#endif
308334
static _Unwind_Reason_Code
309335
unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
310336
_Unwind_Exception *exception_object,

system/lib/libunwind/src/UnwindRegistersRestore.S

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
.text
2121
#endif
2222

23-
#if !defined(__USING_SJLJ_EXCEPTIONS__)
23+
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
2424

2525
#if defined(__i386__)
2626
DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_x86_jumpto)
@@ -629,6 +629,10 @@ Lnovec:
629629

630630
#elif defined(__aarch64__)
631631

632+
#if defined(__ARM_FEATURE_GCS_DEFAULT)
633+
.arch_extension gcs
634+
#endif
635+
632636
//
633637
// extern "C" void __libunwind_Registers_arm64_jumpto(Registers_arm64 *);
634638
//
@@ -680,6 +684,16 @@ DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_arm64_jumpto)
680684
ldr x16, [x0, #0x0F8]
681685
ldp x0, x1, [x0, #0x000] // restore x0,x1
682686
mov sp,x16 // restore sp
687+
#if defined(__ARM_FEATURE_GCS_DEFAULT)
688+
// If GCS is enabled we need to push the address we're returning to onto the
689+
// GCS stack. We can't just return using br, as there won't be a BTI landing
690+
// pad instruction at the destination.
691+
mov x16, #1
692+
chkfeat x16
693+
cbnz x16, Lnogcs
694+
gcspushm x30
695+
Lnogcs:
696+
#endif
683697
ret x30 // jump to pc
684698

685699
#elif defined(__arm__) && !defined(__APPLE__)
@@ -1232,7 +1246,7 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind19Registers_loongarch6jumptoEv)
12321246

12331247
#endif
12341248

1235-
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
1249+
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
12361250

12371251
NO_EXEC_STACK_DIRECTIVE
12381252

system/lib/libunwind/src/UnwindRegistersSave.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
.text
2121
#endif
2222

23-
#if !defined(__USING_SJLJ_EXCEPTIONS__)
23+
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
2424

2525
#if defined(__i386__)
2626

@@ -1177,6 +1177,6 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
11771177

11781178
WEAK_ALIAS(__unw_getcontext, unw_getcontext)
11791179

1180-
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
1180+
#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
11811181

11821182
NO_EXEC_STACK_DIRECTIVE

system/lib/libunwind/src/assembly.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,22 @@
8282
#define PPC64_OPD2
8383
#endif
8484

85-
#if defined(__aarch64__) && defined(__ARM_FEATURE_BTI_DEFAULT)
85+
#if defined(__aarch64__)
86+
#if defined(__ARM_FEATURE_GCS_DEFAULT) && defined(__ARM_FEATURE_BTI_DEFAULT)
87+
// Set BTI, PAC, and GCS gnu property bits
88+
#define GNU_PROPERTY 7
89+
// We indirectly branch to __libunwind_Registers_arm64_jumpto from
90+
// __unw_phase2_resume, so we need to use bti jc.
91+
#define AARCH64_BTI bti jc
92+
#elif defined(__ARM_FEATURE_GCS_DEFAULT)
93+
// Set GCS gnu property bit
94+
#define GNU_PROPERTY 4
95+
#elif defined(__ARM_FEATURE_BTI_DEFAULT)
96+
// Set BTI and PAC gnu property bits
97+
#define GNU_PROPERTY 3
98+
#define AARCH64_BTI bti c
99+
#endif
100+
#ifdef GNU_PROPERTY
86101
.pushsection ".note.gnu.property", "a" SEPARATOR \
87102
.balign 8 SEPARATOR \
88103
.long 4 SEPARATOR \
@@ -91,12 +106,12 @@
91106
.asciz "GNU" SEPARATOR \
92107
.long 0xc0000000 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */ \
93108
.long 4 SEPARATOR \
94-
.long 3 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_BTI AND */ \
95-
/* GNU_PROPERTY_AARCH64_FEATURE_1_PAC */ \
109+
.long GNU_PROPERTY SEPARATOR \
96110
.long 0 SEPARATOR \
97111
.popsection SEPARATOR
98-
#define AARCH64_BTI bti c
99-
#else
112+
#endif
113+
#endif
114+
#if !defined(AARCH64_BTI)
100115
#define AARCH64_BTI
101116
#endif
102117

system/lib/libunwind/src/cet_unwind.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@
3535
} while (0)
3636
#endif
3737

38+
// On AArch64 we use _LIBUNWIND_USE_GCS to indicate that GCS is supported. We
39+
// need to guard any use of GCS instructions with __chkfeat though, as GCS may
40+
// not be enabled.
41+
#if defined(_LIBUNWIND_TARGET_AARCH64) && defined(__ARM_FEATURE_GCS_DEFAULT)
42+
#include <arm_acle.h>
43+
44+
// We can only use GCS if arm_acle.h defines the GCS intrinsics.
45+
#ifdef _CHKFEAT_GCS
46+
#define _LIBUNWIND_USE_GCS 1
47+
#endif
48+
49+
#define _LIBUNWIND_POP_CET_SSP(x) \
50+
do { \
51+
if (__chkfeat(_CHKFEAT_GCS)) { \
52+
unsigned tmp = (x); \
53+
while (tmp--) \
54+
__gcspopm(); \
55+
} \
56+
} while (0)
57+
58+
#endif
59+
3860
extern void *__libunwind_cet_get_registers(unw_cursor_t *);
3961
extern void *__libunwind_cet_get_jump_target(void);
4062

system/lib/libunwind/src/libunwind.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <sanitizer/asan_interface.h>
2727
#endif
2828

29-
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__WASM_EXCEPTIONS__)
29+
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
3030
#include "AddressSpace.hpp"
3131
#include "UnwindCursor.hpp"
3232

@@ -347,8 +347,7 @@ void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
347347
}
348348

349349
#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
350-
#endif // !defined(__USING_SJLJ_EXCEPTIONS__) &&
351-
// !defined(__WASM_EXCEPTIONS__)
350+
#endif // !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
352351

353352
#ifdef __APPLE__
354353

0 commit comments

Comments
 (0)