llvm/compiler-rt/lib/builtins/gcc_personality_v0.c

//===-- gcc_personality_v0.c - Implement __gcc_personality_v0 -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "int_lib.h"
#include <stddef.h>

#include <unwind.h>
#if defined(__arm__) && !defined(__ARM_DWARF_EH__) &&                          \
    !defined(__USING_SJLJ_EXCEPTIONS__)
// When building with older compilers (e.g. clang <3.9), it is possible that we
// have a version of unwind.h which does not provide the EHABI declarations
// which are quired for the C personality to conform to the specification.  In
// order to provide forward compatibility for such compilers, we re-declare the
// necessary interfaces in the helper to permit a standalone compilation of the
// builtins (which contains the C unwinding personality for historical reasons).
#include "unwind-ehabi-helpers.h"
#endif

#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
#include <windows.h>
#include <winnt.h>

EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, void *, PCONTEXT,
                                            PDISPATCHER_CONTEXT,
                                            _Unwind_Personality_Fn);
#endif

// Pointer encodings documented at:
//   http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html

#define DW_EH_PE_omit

#define DW_EH_PE_absptr
#define DW_EH_PE_uleb128
#define DW_EH_PE_udata2
#define DW_EH_PE_udata4
#define DW_EH_PE_udata8
#define DW_EH_PE_sleb128
#define DW_EH_PE_sdata2
#define DW_EH_PE_sdata4
#define DW_EH_PE_sdata8

#define DW_EH_PE_pcrel
#define DW_EH_PE_textrel
#define DW_EH_PE_datarel
#define DW_EH_PE_funcrel
#define DW_EH_PE_aligned
#define DW_EH_PE_indirect

// read a uleb128 encoded value and advance pointer
static size_t readULEB128(const uint8_t **data) {}

// read a pointer encoded value and advance pointer
static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {}

#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) &&                 \
    !defined(__ARM_DWARF_EH__) && !defined(__SEH__)
#define USING_ARM_EHABI
_Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception *,
                                       struct _Unwind_Context *);
#endif

static inline _Unwind_Reason_Code
continueUnwind(struct _Unwind_Exception *exceptionObject,
               struct _Unwind_Context *context) {}

// The C compiler makes references to __gcc_personality_v0 in
// the dwarf unwind information for translation units that use
// __attribute__((cleanup(xx))) on local variables.
// This personality routine is called by the system unwinder
// on each frame as the stack is unwound during a C++ exception
// throw through a C function compiled with -fexceptions.
#if __USING_SJLJ_EXCEPTIONS__
// the setjump-longjump based exceptions personality routine has a
// different name
COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_sj0(
    int version, _Unwind_Action actions, uint64_t exceptionClass,
    struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
#elif USING_ARM_EHABI
// The ARM EHABI personality routine has a different signature.
COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
    _Unwind_State state, struct _Unwind_Exception *exceptionObject,
    struct _Unwind_Context *context)
#elif defined(__SEH__)
static _Unwind_Reason_Code __gcc_personality_imp(
    int version, _Unwind_Action actions, uint64_t exceptionClass,
    struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
#else
COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
    int version, _Unwind_Action actions, uint64_t exceptionClass,
    struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
#endif
{}

#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
COMPILER_RT_ABI EXCEPTION_DISPOSITION
__gcc_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame,
                       PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp) {
  return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, ms_disp,
                               __gcc_personality_imp);
}
#endif