Skip to content

Commit d99707c

Browse files
authored
Update libunwind to LLVM 18.1.2 (#21607)
Most of the changes seem irrelevant to us given that we only use `Uniwnd-wasm.cpp`. Changes related to Wasm are what I submitted upstream for adding the file to `CMakeLists.txt` and fixing some guards: llvm/llvm-project#67770 llvm/llvm-project#73196 llvm/llvm-project#78230
1 parent 6ad821f commit d99707c

16 files changed

+270
-131
lines changed

system/lib/libunwind/include/__libunwind_config.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
# if defined(__linux__)
3737
# define _LIBUNWIND_TARGET_LINUX 1
3838
# endif
39+
# if defined(__HAIKU__)
40+
# define _LIBUNWIND_TARGET_HAIKU 1
41+
# endif
3942
# if defined(__i386__)
4043
# define _LIBUNWIND_TARGET_I386
4144
# define _LIBUNWIND_CONTEXT_SIZE 8
@@ -196,7 +199,7 @@
196199
# define _LIBUNWIND_TARGET_RISCV 1
197200
# define _LIBUNWIND_TARGET_VE 1
198201
# define _LIBUNWIND_TARGET_S390X 1
199-
#define _LIBUNWIND_TARGET_LOONGARCH 1
202+
# define _LIBUNWIND_TARGET_LOONGARCH 1
200203
# define _LIBUNWIND_CONTEXT_SIZE 167
201204
# define _LIBUNWIND_CURSOR_SIZE 204
202205
# define _LIBUNWIND_HIGHEST_DWARF_REGISTER 287

system/lib/libunwind/include/libunwind.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,9 @@ enum {
876876
UNW_MIPS_F29 = 61,
877877
UNW_MIPS_F30 = 62,
878878
UNW_MIPS_F31 = 63,
879+
// HI,LO have been dropped since r6, we keep them here.
880+
// So, when we add DSP/MSA etc, we can use the same register indexes
881+
// for r6 and pre-r6.
879882
UNW_MIPS_HI = 64,
880883
UNW_MIPS_LO = 65,
881884
};

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

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ enum {
108108
// are encoded in the UNWIND_X86_EBP_FRAME_REGISTERS bits as five 3-bit entries.
109109
// Each entry contains which register to restore.
110110
// UNWIND_X86_MODE_STACK_IMMD:
111-
// A "frameless" (EBP not used as frame pointer) function with a small
111+
// A "frameless" (EBP not used as frame pointer) function with a small
112112
// constant stack size. To return, a constant (encoded in the compact
113113
// unwind encoding) is added to the ESP. Then the return is done by
114114
// popping the stack into the pc.
@@ -119,16 +119,16 @@ enum {
119119
// UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION contains which registers were
120120
// saved and their order.
121121
// UNWIND_X86_MODE_STACK_IND:
122-
// A "frameless" (EBP not used as frame pointer) function large constant
122+
// A "frameless" (EBP not used as frame pointer) function large constant
123123
// stack size. This case is like the previous, except the stack size is too
124-
// large to encode in the compact unwind encoding. Instead it requires that
125-
// the function contains "subl $nnnnnnnn,ESP" in its prolog. The compact
124+
// large to encode in the compact unwind encoding. Instead it requires that
125+
// the function contains "subl $nnnnnnnn,ESP" in its prolog. The compact
126126
// encoding contains the offset to the nnnnnnnn value in the function in
127-
// UNWIND_X86_FRAMELESS_STACK_SIZE.
127+
// UNWIND_X86_FRAMELESS_STACK_SIZE.
128128
// UNWIND_X86_MODE_DWARF:
129129
// No compact unwind encoding is available. Instead the low 24-bits of the
130130
// compact encoding is the offset of the DWARF FDE in the __eh_frame section.
131-
// This mode is never used in object files. It is only generated by the
131+
// This mode is never used in object files. It is only generated by the
132132
// linker in final linked images which have only DWARF unwind info for a
133133
// function.
134134
//
@@ -233,36 +233,36 @@ enum {
233233
// For x86_64 there are four modes for the compact unwind encoding:
234234
// UNWIND_X86_64_MODE_RBP_FRAME:
235235
// RBP based frame where RBP is push on stack immediately after return address,
236-
// then RSP is moved to RBP. Thus, to unwind RSP is restored with the current
237-
// EPB value, then RBP is restored by popping off the stack, and the return
236+
// then RSP is moved to RBP. Thus, to unwind RSP is restored with the current
237+
// EPB value, then RBP is restored by popping off the stack, and the return
238238
// is done by popping the stack once more into the pc.
239239
// All non-volatile registers that need to be restored must have been saved
240-
// in a small range in the stack that starts RBP-8 to RBP-2040. The offset/8
240+
// in a small range in the stack that starts RBP-8 to RBP-2040. The offset/8
241241
// is encoded in the UNWIND_X86_64_RBP_FRAME_OFFSET bits. The registers saved
242242
// are encoded in the UNWIND_X86_64_RBP_FRAME_REGISTERS bits as five 3-bit entries.
243-
// Each entry contains which register to restore.
243+
// Each entry contains which register to restore.
244244
// UNWIND_X86_64_MODE_STACK_IMMD:
245-
// A "frameless" (RBP not used as frame pointer) function with a small
246-
// constant stack size. To return, a constant (encoded in the compact
247-
// unwind encoding) is added to the RSP. Then the return is done by
245+
// A "frameless" (RBP not used as frame pointer) function with a small
246+
// constant stack size. To return, a constant (encoded in the compact
247+
// unwind encoding) is added to the RSP. Then the return is done by
248248
// popping the stack into the pc.
249249
// All non-volatile registers that need to be restored must have been saved
250250
// on the stack immediately after the return address. The stack_size/8 is
251251
// encoded in the UNWIND_X86_64_FRAMELESS_STACK_SIZE (max stack size is 2048).
252252
// The number of registers saved is encoded in UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT.
253253
// UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION contains which registers were
254-
// saved and their order.
254+
// saved and their order.
255255
// UNWIND_X86_64_MODE_STACK_IND:
256-
// A "frameless" (RBP not used as frame pointer) function large constant
256+
// A "frameless" (RBP not used as frame pointer) function large constant
257257
// stack size. This case is like the previous, except the stack size is too
258-
// large to encode in the compact unwind encoding. Instead it requires that
259-
// the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact
258+
// large to encode in the compact unwind encoding. Instead it requires that
259+
// the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact
260260
// encoding contains the offset to the nnnnnnnn value in the function in
261-
// UNWIND_X86_64_FRAMELESS_STACK_SIZE.
261+
// UNWIND_X86_64_FRAMELESS_STACK_SIZE.
262262
// UNWIND_X86_64_MODE_DWARF:
263263
// No compact unwind encoding is available. Instead the low 24-bits of the
264264
// compact encoding is the offset of the DWARF FDE in the __eh_frame section.
265-
// This mode is never used in object files. It is only generated by the
265+
// This mode is never used in object files. It is only generated by the
266266
// linker in final linked images which have only DWARF unwind info for a
267267
// function.
268268
//
@@ -307,20 +307,20 @@ enum {
307307
// This is a standard arm64 prolog where FP/LR are immediately pushed on the
308308
// stack, then SP is copied to FP. If there are any non-volatile registers
309309
// saved, then are copied into the stack frame in pairs in a contiguous
310-
// range right below the saved FP/LR pair. Any subset of the five X pairs
310+
// range right below the saved FP/LR pair. Any subset of the five X pairs
311311
// and four D pairs can be saved, but the memory layout must be in register
312-
// number order.
312+
// number order.
313313
// UNWIND_ARM64_MODE_FRAMELESS:
314-
// A "frameless" leaf function, where FP/LR are not saved. The return address
314+
// A "frameless" leaf function, where FP/LR are not saved. The return address
315315
// remains in LR throughout the function. If any non-volatile registers
316316
// are saved, they must be pushed onto the stack before any stack space is
317317
// allocated for local variables. The stack sized (including any saved
318-
// non-volatile registers) divided by 16 is encoded in the bits
318+
// non-volatile registers) divided by 16 is encoded in the bits
319319
// UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK.
320320
// UNWIND_ARM64_MODE_DWARF:
321321
// No compact unwind encoding is available. Instead the low 24-bits of the
322322
// compact encoding is the offset of the DWARF FDE in the __eh_frame section.
323-
// This mode is never used in object files. It is only generated by the
323+
// This mode is never used in object files. It is only generated by the
324324
// linker in final linked images which have only DWARF unwind info for a
325325
// function.
326326
//
@@ -337,19 +337,19 @@ enum {
337337

338338
//
339339
// A compiler can generated compact unwind information for a function by adding
340-
// a "row" to the __LD,__compact_unwind section. This section has the
341-
// S_ATTR_DEBUG bit set, so the section will be ignored by older linkers.
342-
// It is removed by the new linker, so never ends up in final executables.
343-
// This section is a table, initially with one row per function (that needs
340+
// a "row" to the __LD,__compact_unwind section. This section has the
341+
// S_ATTR_DEBUG bit set, so the section will be ignored by older linkers.
342+
// It is removed by the new linker, so never ends up in final executables.
343+
// This section is a table, initially with one row per function (that needs
344344
// unwind info). The table columns and some conceptual entries are:
345345
//
346346
// range-start pointer to start of function/range
347-
// range-length
348-
// compact-unwind-encoding 32-bit encoding
347+
// range-length
348+
// compact-unwind-encoding 32-bit encoding
349349
// personality-function or zero if no personality function
350350
// lsda or zero if no LSDA data
351351
//
352-
// The length and encoding fields are 32-bits. The other are all pointer sized.
352+
// The length and encoding fields are 32-bits. The other are all pointer sized.
353353
//
354354
// In x86_64 assembly, these entry would look like:
355355
//
@@ -372,23 +372,23 @@ enum {
372372
// .quad except_tab1
373373
//
374374
//
375-
// Notes: There is no need for any labels in the the __compact_unwind section.
376-
// The use of the .set directive is to force the evaluation of the
375+
// Notes: There is no need for any labels in the __compact_unwind section.
376+
// The use of the .set directive is to force the evaluation of the
377377
// range-length at assembly time, instead of generating relocations.
378378
//
379-
// To support future compiler optimizations where which non-volatile registers
379+
// To support future compiler optimizations where which non-volatile registers
380380
// are saved changes within a function (e.g. delay saving non-volatiles until
381381
// necessary), there can by multiple lines in the __compact_unwind table for one
382-
// function, each with a different (non-overlapping) range and each with
383-
// different compact unwind encodings that correspond to the non-volatiles
382+
// function, each with a different (non-overlapping) range and each with
383+
// different compact unwind encodings that correspond to the non-volatiles
384384
// saved at that range of the function.
385385
//
386386
// If a particular function is so wacky that there is no compact unwind way
387-
// to encode it, then the compiler can emit traditional DWARF unwind info.
387+
// to encode it, then the compiler can emit traditional DWARF unwind info.
388388
// The runtime will use which ever is available.
389389
//
390-
// Runtime support for compact unwind encodings are only available on 10.6
391-
// and later. So, the compiler should not generate it when targeting pre-10.6.
390+
// Runtime support for compact unwind encodings are only available on 10.6
391+
// and later. So, the compiler should not generate it when targeting pre-10.6.
392392

393393

394394

@@ -402,7 +402,7 @@ enum {
402402
//
403403
// The __TEXT,__unwind_info section is laid out for an efficient two level lookup.
404404
// The header of the section contains a coarse index that maps function address
405-
// to the page (4096 byte block) containing the unwind info for that function.
405+
// to the page (4096 byte block) containing the unwind info for that function.
406406
//
407407

408408
#define UNWIND_SECTION_VERSION 1

system/lib/libunwind/src/AddressSpace.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ static bool checkForUnwindInfoSegment(const Elf_Phdr *phdr, size_t image_base,
414414
cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
415415
cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
416416
if (EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
417-
*cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
418-
hdrInfo)) {
417+
*cbdata->addressSpace, eh_frame_hdr_start,
418+
eh_frame_hdr_start + phdr->p_memsz, hdrInfo)) {
419419
// .eh_frame_hdr records the start of .eh_frame, but not its size.
420420
// Rely on a zero terminator to find the end of the section.
421421
cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
@@ -638,7 +638,8 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
638638
info.dwarf_index_section_length = SIZE_MAX;
639639
EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
640640
if (!EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
641-
*this, info.dwarf_index_section, info.dwarf_index_section_length,
641+
*this, info.dwarf_index_section,
642+
info.dwarf_index_section + info.dwarf_index_section_length,
642643
hdrInfo)) {
643644
return false;
644645
}

system/lib/libunwind/src/DwarfInstructions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class DwarfInstructions {
6868
return (pint_t)((sint_t)registers.getRegister((int)prolog.cfaRegister) +
6969
prolog.cfaRegisterOffset);
7070
if (prolog.cfaExpression != 0)
71-
return evaluateExpression((pint_t)prolog.cfaExpression, addressSpace,
71+
return evaluateExpression((pint_t)prolog.cfaExpression, addressSpace,
7272
registers, 0);
7373
assert(0 && "getCFA(): unknown location");
7474
__builtin_unreachable();

system/lib/libunwind/src/EHHeaderParser.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ template <typename A>
5555
bool EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart,
5656
pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) {
5757
pint_t p = ehHdrStart;
58+
59+
// Ensure that we don't read data beyond the end of .eh_frame_hdr
60+
if (ehHdrEnd - ehHdrStart < 4) {
61+
// Don't print a message for an empty .eh_frame_hdr (this can happen if
62+
// the linker script defines symbols for it even in the empty case).
63+
if (ehHdrEnd == ehHdrStart)
64+
return false;
65+
_LIBUNWIND_LOG("unsupported .eh_frame_hdr at %" PRIx64
66+
": need at least 4 bytes of data but only got %zd",
67+
static_cast<uint64_t>(ehHdrStart),
68+
static_cast<size_t>(ehHdrEnd - ehHdrStart));
69+
return false;
70+
}
5871
uint8_t version = addressSpace.get8(p++);
5972
if (version != 1) {
6073
_LIBUNWIND_LOG("unsupported .eh_frame_hdr version: %" PRIu8 " at %" PRIx64,

system/lib/libunwind/src/FrameHeaderCache.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
class _LIBUNWIND_HIDDEN FrameHeaderCache {
3333
struct CacheEntry {
34-
uintptr_t LowPC() { return Info.dso_base; };
35-
uintptr_t HighPC() { return Info.dso_base + Info.text_segment_length; };
34+
uintptr_t LowPC() { return Info.dso_base; }
35+
uintptr_t HighPC() { return Info.dso_base + Info.text_segment_length; }
3636
UnwindInfoSections Info;
3737
CacheEntry *Next;
3838
};
@@ -41,7 +41,7 @@ class _LIBUNWIND_HIDDEN FrameHeaderCache {
4141

4242
// Can't depend on the C++ standard library in libunwind, so use an array to
4343
// allocate the entries, and two linked lists for ordering unused and recently
44-
// used entries. FIXME: Would the the extra memory for a doubly-linked list
44+
// used entries. FIXME: Would the extra memory for a doubly-linked list
4545
// be better than the runtime cost of traversing a very short singly-linked
4646
// list on a cache miss? The entries themselves are all small and consecutive,
4747
// so unlikely to cause page faults when following the pointers. The memory

system/lib/libunwind/src/Registers.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ class _LIBUNWIND_HIDDEN Registers_ppc {
619619
void setIP(uint32_t value) { _registers.__srr0 = value; }
620620
uint64_t getCR() const { return _registers.__cr; }
621621
void setCR(uint32_t value) { _registers.__cr = value; }
622+
uint64_t getLR() const { return _registers.__lr; }
623+
void setLR(uint32_t value) { _registers.__lr = value; }
622624

623625
private:
624626
struct ppc_thread_state_t {
@@ -1189,6 +1191,8 @@ class _LIBUNWIND_HIDDEN Registers_ppc64 {
11891191
void setIP(uint64_t value) { _registers.__srr0 = value; }
11901192
uint64_t getCR() const { return _registers.__cr; }
11911193
void setCR(uint64_t value) { _registers.__cr = value; }
1194+
uint64_t getLR() const { return _registers.__lr; }
1195+
void setLR(uint64_t value) { _registers.__lr = value; }
11921196

11931197
private:
11941198
struct ppc64_thread_state_t {
@@ -2869,7 +2873,7 @@ inline bool Registers_mips_o32::validRegister(int regNum) const {
28692873
return false;
28702874
if (regNum <= UNW_MIPS_R31)
28712875
return true;
2872-
#if __mips_isa_rev != 6
2876+
#if __mips_isa_rev < 6
28732877
if (regNum == UNW_MIPS_HI)
28742878
return true;
28752879
if (regNum == UNW_MIPS_LO)
@@ -2903,10 +2907,12 @@ inline uint32_t Registers_mips_o32::getRegister(int regNum) const {
29032907
return _registers.__pc;
29042908
case UNW_REG_SP:
29052909
return _registers.__r[29];
2910+
#if __mips_isa_rev < 6
29062911
case UNW_MIPS_HI:
29072912
return _registers.__hi;
29082913
case UNW_MIPS_LO:
29092914
return _registers.__lo;
2915+
#endif
29102916
}
29112917
_LIBUNWIND_ABORT("unsupported mips_o32 register");
29122918
}
@@ -2936,11 +2942,13 @@ inline void Registers_mips_o32::setRegister(int regNum, uint32_t value) {
29362942
case UNW_REG_SP:
29372943
_registers.__r[29] = value;
29382944
return;
2945+
#if __mips_isa_rev < 6
29392946
case UNW_MIPS_HI:
29402947
_registers.__hi = value;
29412948
return;
29422949
case UNW_MIPS_LO:
29432950
_registers.__lo = value;
2951+
#endif
29442952
return;
29452953
}
29462954
_LIBUNWIND_ABORT("unsupported mips_o32 register");
@@ -3120,10 +3128,12 @@ inline const char *Registers_mips_o32::getRegisterName(int regNum) {
31203128
return "$f30";
31213129
case UNW_MIPS_F31:
31223130
return "$f31";
3131+
#if __mips_isa_rev < 6
31233132
case UNW_MIPS_HI:
31243133
return "$hi";
31253134
case UNW_MIPS_LO:
31263135
return "$lo";
3136+
#endif
31273137
default:
31283138
return "unknown register";
31293139
}
@@ -3193,7 +3203,7 @@ inline bool Registers_mips_newabi::validRegister(int regNum) const {
31933203
return false;
31943204
if (regNum <= UNW_MIPS_R31)
31953205
return true;
3196-
#if __mips_isa_rev != 6
3206+
#if __mips_isa_rev < 6
31973207
if (regNum == UNW_MIPS_HI)
31983208
return true;
31993209
if (regNum == UNW_MIPS_LO)
@@ -3212,10 +3222,12 @@ inline uint64_t Registers_mips_newabi::getRegister(int regNum) const {
32123222
return _registers.__pc;
32133223
case UNW_REG_SP:
32143224
return _registers.__r[29];
3225+
#if __mips_isa_rev < 6
32153226
case UNW_MIPS_HI:
32163227
return _registers.__hi;
32173228
case UNW_MIPS_LO:
32183229
return _registers.__lo;
3230+
#endif
32193231
}
32203232
_LIBUNWIND_ABORT("unsupported mips_newabi register");
32213233
}
@@ -3233,12 +3245,14 @@ inline void Registers_mips_newabi::setRegister(int regNum, uint64_t value) {
32333245
case UNW_REG_SP:
32343246
_registers.__r[29] = value;
32353247
return;
3248+
#if __mips_isa_rev < 6
32363249
case UNW_MIPS_HI:
32373250
_registers.__hi = value;
32383251
return;
32393252
case UNW_MIPS_LO:
32403253
_registers.__lo = value;
32413254
return;
3255+
#endif
32423256
}
32433257
_LIBUNWIND_ABORT("unsupported mips_newabi register");
32443258
}
@@ -3417,10 +3431,12 @@ inline const char *Registers_mips_newabi::getRegisterName(int regNum) {
34173431
return "$f30";
34183432
case UNW_MIPS_F31:
34193433
return "$f31";
3434+
#if __mips_isa_rev < 6
34203435
case UNW_MIPS_HI:
34213436
return "$hi";
34223437
case UNW_MIPS_LO:
34233438
return "$lo";
3439+
#endif
34243440
default:
34253441
return "unknown register";
34263442
}

0 commit comments

Comments
 (0)