diff options
author | Iain Buclaw <ibuclaw@gcc.gnu.org> | 2018-10-28 19:51:47 +0000 |
---|---|---|
committer | Iain Buclaw <ibuclaw@gcc.gnu.org> | 2018-10-28 19:51:47 +0000 |
commit | b4c522fabd0df7be08882d2207df8b2765026110 (patch) | |
tree | b5ffc312b0a441c1ba24323152aec463fdbe5e9f /libphobos/libdruntime/gcc | |
parent | 01ce9e31a02c8039d88e90f983735104417bf034 (diff) | |
download | gcc-b4c522fabd0df7be08882d2207df8b2765026110.zip gcc-b4c522fabd0df7be08882d2207df8b2765026110.tar.gz gcc-b4c522fabd0df7be08882d2207df8b2765026110.tar.bz2 |
Add D front-end, libphobos library, and D2 testsuite.
ChangeLog:
* Makefile.def (target_modules): Add libphobos.
(flags_to_pass): Add GDC, GDCFLAGS, GDC_FOR_TARGET and
GDCFLAGS_FOR_TARGET.
(dependencies): Make libphobos depend on libatomic, libbacktrace
configure, and zlib configure.
(language): Add language d.
* Makefile.in: Rebuild.
* Makefile.tpl (BUILD_EXPORTS): Add GDC and GDCFLAGS.
(HOST_EXPORTS): Add GDC.
(POSTSTAGE1_HOST_EXPORTS): Add GDC and GDC_FOR_BUILD.
(BASE_TARGET_EXPORTS): Add GDC.
(GDC_FOR_BUILD, GDC, GDCFLAGS): New variables.
(GDC_FOR_TARGET, GDC_FLAGS_FOR_TARGET): New variables.
(EXTRA_HOST_FLAGS): Add GDC.
(STAGE1_FLAGS_TO_PASS): Add GDC.
(EXTRA_TARGET_FLAGS): Add GDC and GDCFLAGS.
* config-ml.in: Treat GDC and GDCFLAGS like other compiler/flag
environment variables.
* configure: Rebuild.
* configure.ac: Add target-libphobos to target_libraries. Set and
substitute GDC_FOR_BUILD and GDC_FOR_TARGET.
config/ChangeLog:
* multi.m4: Set GDC.
gcc/ChangeLog:
* Makefile.in (tm_d_file_list, tm_d_include_list): New variables.
(TM_D_H, D_TARGET_DEF, D_TARGET_H, D_TARGET_OBJS): New variables.
(tm_d.h, cs-tm_d.h, default-d.o): New rules.
(d/d-target-hooks-def.h, s-d-target-hooks-def-h): New rules.
(s-tm-texi): Also check timestamp on d-target.def.
(generated_files): Add TM_D_H and d-target-hooks-def.h.
(build/genhooks.o): Also depend on D_TARGET_DEF.
* config.gcc (tm_d_file, d_target_objs, target_has_targetdm): New
variables.
* config/aarch64/aarch64-d.c: New file.
* config/aarch64/aarch64-linux.h (GNU_USER_TARGET_D_CRITSEC_SIZE):
Define.
* config/aarch64/aarch64-protos.h (aarch64_d_target_versions): New
prototype.
* config/aarch64/aarch64.h (TARGET_D_CPU_VERSIONS): Define.
* config/aarch64/t-aarch64 (aarch64-d.o): New rule.
* config/arm/arm-d.c: New file.
* config/arm/arm-protos.h (arm_d_target_versions): New prototype.
* config/arm/arm.h (TARGET_D_CPU_VERSIONS): Define.
* config/arm/linux-eabi.h (EXTRA_TARGET_D_OS_VERSIONS): Define.
* config/arm/t-arm (arm-d.o): New rule.
* config/default-d.c: New file.
* config/glibc-d.c: New file.
* config/gnu.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/i386/i386-d.c: New file.
* config/i386/i386-protos.h (ix86_d_target_versions): New prototype.
* config/i386/i386.h (TARGET_D_CPU_VERSIONS): Define.
* config/i386/linux-common.h (EXTRA_TARGET_D_OS_VERSIONS): Define.
(GNU_USER_TARGET_D_CRITSEC_SIZE): Define.
* config/i386/t-i386 (i386-d.o): New rule.
* config/kfreebsd-gnu.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/kopensolaris-gnu.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/linux-android.h (ANDROID_TARGET_D_OS_VERSIONS): Define.
* config/linux.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/mips/linux-common.h (EXTRA_TARGET_D_OS_VERSIONS): Define.
* config/mips/mips-d.c: New file.
* config/mips/mips-protos.h (mips_d_target_versions): New prototype.
* config/mips/mips.h (TARGET_D_CPU_VERSIONS): Define.
* config/mips/t-mips (mips-d.o): New rule.
* config/powerpcspe/linux.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/powerpcspe/linux64.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/powerpcspe/powerpcspe-d.c: New file.
* config/powerpcspe/powerpcspe-protos.h (rs6000_d_target_versions):
New prototype.
* config/powerpcspe/powerpcspe.c (rs6000_output_function_epilogue):
Support GNU D by using 0 as the language type.
* config/powerpcspe/powerpcspe.h (TARGET_D_CPU_VERSIONS): Define.
* config/powerpcspe/t-powerpcspe (powerpcspe-d.o): New rule.
* config/riscv/riscv-d.c: New file.
* config/riscv/riscv-protos.h (riscv_d_target_versions): New
prototype.
* config/riscv/riscv.h (TARGET_D_CPU_VERSIONS): Define.
* config/riscv/t-riscv (riscv-d.o): New rule.
* config/rs6000/linux.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/rs6000/linux64.h (GNU_USER_TARGET_D_OS_VERSIONS): Define.
* config/rs6000/rs6000-d.c: New file.
* config/rs6000/rs6000-protos.h (rs6000_d_target_versions): New
prototype.
* config/rs6000/rs6000.c (rs6000_output_function_epilogue):
Support GNU D by using 0 as the language type.
* config/rs6000/rs6000.h (TARGET_D_CPU_VERSIONS): Define.
* config/rs6000/t-rs6000 (rs6000-d.o): New rule.
* config/s390/s390-d.c: New file.
* config/s390/s390-protos.h (s390_d_target_versions): New prototype.
* config/s390/s390.h (TARGET_D_CPU_VERSIONS): Define.
* config/s390/t-s390 (s390-d.o): New rule.
* config/sparc/sparc-d.c: New file.
* config/sparc/sparc-protos.h (sparc_d_target_versions): New
prototype.
* config/sparc/sparc.h (TARGET_D_CPU_VERSIONS): Define.
* config/sparc/t-sparc (sparc-d.o): New rule.
* config/t-glibc (glibc-d.o): New rule.
* configure: Regenerated.
* configure.ac (tm_d_file): New variable.
(tm_d_file_list, tm_d_include_list, d_target_objs): Add substitutes.
* doc/contrib.texi (Contributors): Add self for the D frontend.
* doc/frontends.texi (G++ and GCC): Mention D as a supported language.
* doc/install.texi (Configuration): Mention libphobos as an option for
--enable-shared. Mention d as an option for --enable-languages.
(Testing): Mention check-d as a target.
* doc/invoke.texi (Overall Options): Mention .d, .dd, and .di as file
name suffixes. Mention d as a -x option.
* doc/sourcebuild.texi (Top Level): Mention libphobos.
* doc/standards.texi (Standards): Add section on D language.
* doc/tm.texi: Regenerated.
* doc/tm.texi.in: Add @node for D language and ABI, and @hook for
TARGET_CPU_VERSIONS, TARGET_D_OS_VERSIONS, and TARGET_D_CRITSEC_SIZE.
* dwarf2out.c (is_dlang): New function.
(gen_compile_unit_die): Use DW_LANG_D for D.
(declare_in_namespace): Return module die for D, instead of adding
extra declarations into the namespace.
(gen_namespace_die): Generate DW_TAG_module for D.
(gen_decl_die): Handle CONST_DECLSs for D.
(dwarf2out_decl): Likewise.
(prune_unused_types_walk_local_classes): Handle DW_tag_interface_type.
(prune_unused_types_walk): Handle DW_tag_interface_type same as other
kinds of aggregates.
* gcc.c (default_compilers): Add entries for .d, .dd and .di.
* genhooks.c: Include d/d-target.def.
gcc/po/ChangeLog:
* EXCLUDES: Add sources from d/dmd.
gcc/testsuite/ChangeLog:
* gcc.misc-tests/help.exp: Add D to option descriptions check.
* gdc.dg/asan/asan.exp: New file.
* gdc.dg/asan/gdc272.d: New test.
* gdc.dg/compilable.d: New test.
* gdc.dg/dg.exp: New file.
* gdc.dg/gdc254.d: New test.
* gdc.dg/gdc260.d: New test.
* gdc.dg/gdc270a.d: New test.
* gdc.dg/gdc270b.d: New test.
* gdc.dg/gdc282.d: New test.
* gdc.dg/gdc283.d: New test.
* gdc.dg/imports/gdc170.d: New test.
* gdc.dg/imports/gdc231.d: New test.
* gdc.dg/imports/gdc239.d: New test.
* gdc.dg/imports/gdc241a.d: New test.
* gdc.dg/imports/gdc241b.d: New test.
* gdc.dg/imports/gdc251a.d: New test.
* gdc.dg/imports/gdc251b.d: New test.
* gdc.dg/imports/gdc253.d: New test.
* gdc.dg/imports/gdc254a.d: New test.
* gdc.dg/imports/gdc256.d: New test.
* gdc.dg/imports/gdc27.d: New test.
* gdc.dg/imports/gdcpkg256/package.d: New test.
* gdc.dg/imports/runnable.d: New test.
* gdc.dg/link.d: New test.
* gdc.dg/lto/lto.exp: New file.
* gdc.dg/lto/ltotests_0.d: New test.
* gdc.dg/lto/ltotests_1.d: New test.
* gdc.dg/runnable.d: New test.
* gdc.dg/simd.d: New test.
* gdc.test/gdc-test.exp: New file.
* lib/gdc-dg.exp: New file.
* lib/gdc.exp: New file.
libphobos/ChangeLog:
* Makefile.am: New file.
* Makefile.in: New file.
* acinclude.m4: New file.
* aclocal.m4: New file.
* config.h.in: New file.
* configure: New file.
* configure.ac: New file.
* d_rules.am: New file.
* libdruntime/Makefile.am: New file.
* libdruntime/Makefile.in: New file.
* libdruntime/__entrypoint.di: New file.
* libdruntime/__main.di: New file.
* libdruntime/gcc/attribute.d: New file.
* libdruntime/gcc/backtrace.d: New file.
* libdruntime/gcc/builtins.d: New file.
* libdruntime/gcc/config.d.in: New file.
* libdruntime/gcc/deh.d: New file.
* libdruntime/gcc/libbacktrace.d.in: New file.
* libdruntime/gcc/unwind/arm.d: New file.
* libdruntime/gcc/unwind/arm_common.d: New file.
* libdruntime/gcc/unwind/c6x.d: New file.
* libdruntime/gcc/unwind/generic.d: New file.
* libdruntime/gcc/unwind/package.d: New file.
* libdruntime/gcc/unwind/pe.d: New file.
* m4/autoconf.m4: New file.
* m4/druntime.m4: New file.
* m4/druntime/cpu.m4: New file.
* m4/druntime/libraries.m4: New file.
* m4/druntime/os.m4: New file.
* m4/gcc_support.m4: New file.
* m4/gdc.m4: New file.
* m4/libtool.m4: New file.
* src/Makefile.am: New file.
* src/Makefile.in: New file.
* src/libgphobos.spec.in: New file.
* testsuite/Makefile.am: New file.
* testsuite/Makefile.in: New file.
* testsuite/config/default.exp: New file.
* testsuite/lib/libphobos-dg.exp: New file.
* testsuite/lib/libphobos.exp: New file.
* testsuite/testsuite_flags.in: New file.
From-SVN: r265573
Diffstat (limited to 'libphobos/libdruntime/gcc')
-rw-r--r-- | libphobos/libdruntime/gcc/attribute.d | 33 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/backtrace.d | 613 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/builtins.d | 44 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/config.d.in | 48 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/deh.d | 991 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/libbacktrace.d.in | 73 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/unwind/arm.d | 71 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/unwind/arm_common.d | 244 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/unwind/c6x.d | 52 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/unwind/generic.d | 274 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/unwind/package.d | 37 | ||||
-rw-r--r-- | libphobos/libdruntime/gcc/unwind/pe.d | 238 |
12 files changed, 2718 insertions, 0 deletions
diff --git a/libphobos/libdruntime/gcc/attribute.d b/libphobos/libdruntime/gcc/attribute.d new file mode 100644 index 0000000..af3757c --- /dev/null +++ b/libphobos/libdruntime/gcc/attribute.d @@ -0,0 +1,33 @@ +// GNU D Compiler attribute support declarations. +// Copyright (C) 2013-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +module gcc.attribute; + +private struct Attribute(A...) +{ + A args; +} + +auto attribute(A...)(A args) if (A.length > 0 && is(A[0] == string)) +{ + return Attribute!A(args); +} diff --git a/libphobos/libdruntime/gcc/backtrace.d b/libphobos/libdruntime/gcc/backtrace.d new file mode 100644 index 0000000..cf5554e --- /dev/null +++ b/libphobos/libdruntime/gcc/backtrace.d @@ -0,0 +1,613 @@ +// GNU D Compiler routines for stack backtrace support. +// Copyright (C) 2013-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +module gcc.backtrace; + +import gcc.libbacktrace; + +version (Posix) +{ + // NOTE: The first 5 frames with the current implementation are + // inside core.runtime and the object code, so eliminate + // these for readability. The alternative would be to + // exclude the first N frames that are in a list of + // mangled function names. + private enum FIRSTFRAME = 5; +} +else +{ + // NOTE: On Windows, the number of frames to exclude is based on + // whether the exception is user or system-generated, so + // it may be necessary to exclude a list of function names + // instead. + private enum FIRSTFRAME = 0; +} + +// Max size per line of the traceback. +private enum MAX_BUFSIZE = 1536; + +static if (BACKTRACE_SUPPORTED && !BACKTRACE_USES_MALLOC) +{ + import core.stdc.stdint, core.stdc.string, core.stdc.stdio; + private enum MAXFRAMES = 128; + + extern(C) int simpleCallback(void* data, uintptr_t pc) + { + auto context = cast(LibBacktrace)data; + + if (context.numPCs == MAXFRAMES) + return 1; + + context.pcs[context.numPCs++] = pc; + return 0; + } + + /* + * Used for backtrace_create_state and backtrace_simple + */ + extern(C) void simpleErrorCallback(void* data, const(char)* msg, int errnum) + { + if (data) // context is not available in backtrace_create_state + { + auto context = cast(LibBacktrace)data; + strncpy(context.errorBuf.ptr, msg, context.errorBuf.length - 1); + context.error = errnum; + } + } + + /* + * Used for backtrace_pcinfo + */ + extern(C) int pcinfoCallback(void* data, uintptr_t pc, const(char)* filename, + int lineno, const(char)* func) + { + auto context = cast(SymbolCallbackInfo*)data; + + // Try to get the function name via backtrace_syminfo + if (func is null) + { + SymbolCallbackInfo2 info; + info.base = context; + info.filename = filename; + info.lineno = lineno; + if (backtrace_syminfo(context.state, pc, &syminfoCallback2, null, &info) != 0) + { + return context.retval; + } + } + + auto sym = SymbolOrError(0, SymbolInfo(func, filename, lineno, cast(void*)pc)); + context.retval = context.applyCB(context.num, sym); + context.num++; + + return context.retval; + } + + /* + * Used for backtrace_pcinfo and backtrace_syminfo + */ + extern(C) void pcinfoErrorCallback(void* data, const(char)* msg, int errnum) + { + auto context = cast(SymbolCallbackInfo*)data; + + if (errnum == -1) + { + context.noInfo = true; + return; + } + + SymbolOrError symError; + symError.errnum = errnum; + symError.msg = msg; + + size_t i = 0; + context.retval = context.applyCB(i, symError); + } + + /* + * Used for backtrace_syminfo (in opApply) + */ + extern(C) void syminfoCallback(void* data, uintptr_t pc, + const(char)* symname, uintptr_t symval) + { + auto context = cast(SymbolCallbackInfo*)data; + + auto sym = SymbolOrError(0, SymbolInfo(symname, null, 0, cast(void*)pc)); + context.retval = context.applyCB(context.num, sym); + + context.num++; + } + + /* + * This callback is used if backtrace_syminfo is called from the pcinfoCallback + * callback. It merges it's information with the information from pcinfoCallback. + */ + extern(C) void syminfoCallback2(void* data, uintptr_t pc, + const(char)* symname, uintptr_t symval) + { + auto context = cast(SymbolCallbackInfo2*)data; + + auto sym = SymbolOrError(0, SymbolInfo(symname, context.filename, context.lineno, + cast(void*)pc)); + context.base.retval = context.base.applyCB(context.base.num, sym); + + context.base.num++; + } + + /* + * The callback type used with the opApply overload which returns a SymbolOrError + */ + private alias int delegate(ref size_t, ref SymbolOrError) ApplyCallback; + + /* + * Passed to syminfoCallback, pcinfoCallback and pcinfoErrorCallback + */ + struct SymbolCallbackInfo + { + bool noInfo = false; // True if debug info / symbol table is not available + size_t num = 0; // Counter for opApply + int retval; // Value returned by applyCB + backtrace_state* state; + + // info.fileName / funcName / errmsg may become invalid after this delegate returned + ApplyCallback applyCB; + + void reset() + { + noInfo = false; + num = 0; + } + } + + /* + * Passed to the syminfoCallback2 callback. That function merges it's + * funcName with this information and updates base as all other callbacks do. + */ + struct SymbolCallbackInfo2 + { + SymbolCallbackInfo* base; + const(char)* filename; + int lineno; + } + + /* + * Contains a valid symbol or an error message if errnum is != 0. + */ + struct SymbolOrError + { + int errnum; // == 0: No error + union + { + SymbolInfo symbol; + const(char)* msg; + } + } + + // FIXME: state is never freed as libbacktrace doesn't provide a free function... + public class LibBacktrace : Throwable.TraceInfo + { + enum MaxAlignment = (void*).alignof; + + static void initLibBacktrace() + { + if (!initialized) + { + state = backtrace_create_state(null, false, &simpleErrorCallback, null); + initialized = true; + } + } + + this(int firstFrame = FIRSTFRAME) + { + _firstFrame = firstFrame; + + initLibBacktrace(); + + if (state) + { + backtrace_simple(state, _firstFrame, &simpleCallback, + &simpleErrorCallback, cast(void*)this); + } + } + + override int opApply(scope int delegate(ref const(char[])) dg) const + { + return opApply( + (ref size_t, ref const(char[]) buf) + { + return dg(buf); + } + ); + } + + override int opApply(scope int delegate(ref size_t, ref const(char[])) dg) const + { + return opApply( + (ref size_t i, ref SymbolOrError sym) + { + char[MAX_BUFSIZE] buffer = void; + char[] msg; + if (sym.errnum != 0) + { + auto retval = snprintf(buffer.ptr, buffer.length, + "libbacktrace error: '%s' errno: %d", sym.msg, sym.errnum); + if (retval >= buffer.length) + retval = buffer.length - 1; // Ignore zero terminator + if (retval > 0) + msg = buffer[0 .. retval]; + return dg(i, msg); + } + else + { + msg = formatLine(sym.symbol, buffer); + int ret = dg(i, msg); + + if (!ret && sym.symbol.funcName && strcmp(sym.symbol.funcName, "_Dmain") == 0) + return 1; + return ret; + } + } + ); + } + + int opApply(scope ApplyCallback dg) const + { + initLibBacktrace(); + + // If backtrace_simple produced an error report it and exit + if (!state || error != 0) + { + size_t pos = 0; + SymbolOrError symError; + if (!state) + { + symError.msg = "libbacktrace failed to initialize\0"; + symError.errnum = 1; + } + else + { + symError.errnum = error; + symError.msg = errorBuf.ptr; + } + + return dg(pos, symError); + } + + SymbolCallbackInfo cinfo; + cinfo.applyCB = dg; + cinfo.state = cast(backtrace_state*)state; + + // Try using debug info first + foreach (i, pc; pcs[0 .. numPCs]) + { + // FIXME: We may violate const guarantees here... + if (backtrace_pcinfo(cast(backtrace_state*)state, pc, &pcinfoCallback, + &pcinfoErrorCallback, &cinfo) != 0) + { + break; // User delegate requested abort or no debug info at all + } + } + + // If no error or other error which has already been reported via callback + if (!cinfo.noInfo) + return cinfo.retval; + + // Try using symbol table + cinfo.reset(); + foreach (pc; pcs[0 .. numPCs]) + { + if (backtrace_syminfo(cast(backtrace_state*)state, pc, &syminfoCallback, + &pcinfoErrorCallback, &cinfo) == 0) + { + break; + } + } + + if (!cinfo.noInfo) + return cinfo.retval; + + // No symbol table + foreach (i, pc; pcs[0 .. numPCs]) + { + auto sym = SymbolOrError(0, SymbolInfo(null, null, 0, cast(void*)pc)); + if (auto ret = dg(i, sym) != 0) + return ret; + } + + return 0; + } + + override string toString() const + { + string buf; + foreach (i, const(char[]) line; this) + buf ~= i ? "\n" ~ line : line; + return buf; + } + + private: + static backtrace_state* state = null; + static bool initialized = false; + size_t numPCs = 0; + uintptr_t[MAXFRAMES] pcs; + + int error = 0; + int _firstFrame = 0; + char[128] errorBuf = "\0"; + } +} +else +{ + /* + * Our fallback backtrace implementation using libgcc's unwind + * and backtrace support. In theory libbacktrace should be available + * everywhere where this code works. We keep it anyway till libbacktrace + * is well-tested. + */ + public class UnwindBacktrace : Throwable.TraceInfo + { + this(int firstFrame = FIRSTFRAME) + { + _firstFrame = firstFrame; + _callstack = getBacktrace(); + _framelist = getBacktraceSymbols(_callstack); + } + + override int opApply(scope int delegate(ref const(char[])) dg) const + { + return opApply( + (ref size_t, ref const(char[]) buf) + { + return dg(buf); + } + ); + } + + override int opApply(scope int delegate(ref size_t, ref const(char[])) dg) const + { + char[MAX_BUFSIZE] buffer = void; + int ret = 0; + + for (int i = _firstFrame; i < _framelist.entries; ++i) + { + auto pos = cast(size_t)(i - _firstFrame); + auto msg = formatLine(_framelist.symbols[i], buffer); + ret = dg(pos, msg); + if (ret) + break; + } + return ret; + } + + override string toString() const + { + string buf; + foreach (i, line; this) + buf ~= i ? "\n" ~ line : line; + return buf; + } + + private: + BTSymbolData _framelist; + UnwindBacktraceData _callstack; + int _firstFrame = 0; + } + + // Implementation details +private: + import gcc.unwind; + + version (linux) + import core.sys.linux.dlfcn; + else version (OSX) + import core.sys.darwin.dlfcn; + else version (FreeBSD) + import core.sys.freebsd.dlfcn; + else version (NetBSD) + import core.sys.netbsd.dlfcn; + else version (Solaris) + import core.sys.netbsd.dlfcn; + else version (Posix) + import core.sys.posix.dlfcn; + + private enum MAXFRAMES = 128; + + struct UnwindBacktraceData + { + void*[MAXFRAMES] callstack; + int numframes = 0; + } + + struct BTSymbolData + { + size_t entries; + SymbolInfo[MAXFRAMES] symbols; + } + + static extern (C) _Unwind_Reason_Code unwindCB(_Unwind_Context *ctx, void *d) + { + UnwindBacktraceData* bt = cast(UnwindBacktraceData*)d; + if (bt.numframes >= MAXFRAMES) + return _URC_NO_REASON; + + bt.callstack[bt.numframes] = cast(void*)_Unwind_GetIP(ctx); + bt.numframes++; + return _URC_NO_REASON; + } + + UnwindBacktraceData getBacktrace() + { + UnwindBacktraceData stackframe; + _Unwind_Backtrace(&unwindCB, &stackframe); + return stackframe; + } + + BTSymbolData getBacktraceSymbols(UnwindBacktraceData data) + { + BTSymbolData symData; + + for (auto i = 0; i < data.numframes; i++) + { + static if ( __traits(compiles, Dl_info)) + { + Dl_info funcInfo; + + if (data.callstack[i] !is null && dladdr(data.callstack[i], &funcInfo) != 0) + { + symData.symbols[symData.entries].funcName = funcInfo.dli_sname; + + symData.symbols[symData.entries].address = data.callstack[i]; + symData.entries++; + } + else + { + symData.symbols[symData.entries].address = data.callstack[i]; + symData.entries++; + } + } + else + { + symData.symbols[symData.entries].address = data.callstack[i]; + symData.entries++; + } + } + + return symData; + } +} + +/* + * Struct representing a symbol (function) in the backtrace + */ +struct SymbolInfo +{ + const(char)* funcName, fileName; + size_t line; + const(void)* address; +} + +/* + * Format one output line for symbol sym. + * Returns a slice of buffer. + */ +char[] formatLine(const SymbolInfo sym, return ref char[MAX_BUFSIZE] buffer) +{ + import core.demangle, core.stdc.config; + import core.stdc.stdio : snprintf, printf; + import core.stdc.string : strlen; + + size_t bufferLength = 0; + + void appendToBuffer(Args...)(const(char)* format, Args args) + { + const count = snprintf(buffer.ptr + bufferLength, + buffer.length - bufferLength, + format, args); + assert(count >= 0); + bufferLength += count; + if (bufferLength >= buffer.length) + bufferLength = buffer.length - 1; + } + + + if (sym.fileName is null) + appendToBuffer("??:? "); + else + appendToBuffer("%s:%d ", sym.fileName, sym.line); + + if (sym.funcName is null) + appendToBuffer("???"); + else + { + char[1024] symbol = void; + auto demangled = demangle(sym.funcName[0 .. strlen(sym.funcName)], symbol); + + if (demangled.length > 0) + appendToBuffer("%.*s ", cast(int) demangled.length, demangled.ptr); + } + + version (D_LP64) + appendToBuffer("[0x%llx]", cast(size_t)sym.address); + else + appendToBuffer("[0x%x]", cast(size_t)sym.address); + + return buffer[0 .. bufferLength]; +} + + +unittest +{ + char[MAX_BUFSIZE] sbuf = '\0'; + char[] result; + string longString; + for (size_t i = 0; i < 60; i++) + longString ~= "abcdefghij"; + longString ~= '\0'; + + auto symbol = SymbolInfo(null, null, 0, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo(longString.ptr, null, 0, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo("func", "test.d", 0, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo("func", longString.ptr, 0, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo(longString.ptr, "test.d", 0, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo(longString.ptr, longString.ptr, 0, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo("func", "test.d", 1000, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo(null, (longString[0..500] ~ '\0').ptr, 100000000, null); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo("func", "test.d", 0, cast(void*)0x100000); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo("func", null, 0, cast(void*)0x100000); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo(null, "test.d", 0, cast(void*)0x100000); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); + + symbol = SymbolInfo(longString.ptr, "test.d", 0, cast(void*)0x100000); + result = formatLine(symbol, sbuf); + assert(result.length < MAX_BUFSIZE && result.ptr[result.length] == '\0' && sbuf[$-1] == '\0'); +} diff --git a/libphobos/libdruntime/gcc/builtins.d b/libphobos/libdruntime/gcc/builtins.d new file mode 100644 index 0000000..a84d60b --- /dev/null +++ b/libphobos/libdruntime/gcc/builtins.d @@ -0,0 +1,44 @@ +/* GNU D Compiler bindings for built-in functions and types. + Copyright (C) 2006-2018 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +Under Section 7 of GPL version 3, you are granted additional +permissions described in the GCC Runtime Library Exception, version +3.1, as published by the Free Software Foundation. + +You should have received a copy of the GNU General Public License and +a copy of the GCC Runtime Library Exception along with this program; +see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +<http://www.gnu.org/licenses/>. */ + +/** + Declarations are automatically created by the compiler. All + declarations start with "__builtin_". Refer to _builtins.def in the + GCC source for a list of functions. Not all of the functions are + supported. + + In addition to built-in functions, the following types are defined. + + $(TABLE + $(TR $(TD ___builtin_va_list) $(TD The target's va_list type )) + $(TR $(TD ___builtin_clong ) $(TD The D equivalent of the target's + C "long" type )) + $(TR $(TD ___builtin_culong ) $(TD The D equivalent of the target's + C "unsigned long" type )) + $(TR $(TD ___builtin_machine_int ) $(TD Signed word-sized integer )) + $(TR $(TD ___builtin_machine_uint) $(TD Unsigned word-sized integer )) + $(TR $(TD ___builtin_pointer_int ) $(TD Signed pointer-sized integer )) + $(TR $(TD ___builtin_pointer_uint) $(TD Unsigned pointer-sized integer )) + ) + */ + +module gcc.builtins; diff --git a/libphobos/libdruntime/gcc/config.d.in b/libphobos/libdruntime/gcc/config.d.in new file mode 100644 index 0000000..89f807d --- /dev/null +++ b/libphobos/libdruntime/gcc/config.d.in @@ -0,0 +1,48 @@ +// GNU D Compiler configure constants. +// Copyright (C) 2015-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +module gcc.config; + +// Does platform define __ARM_EABI_UNWINDER__ +enum GNU_ARM_EABI_Unwinder = @DCFG_ARM_EABI_UNWINDER@; + +// Map from thread model to thread interface. +enum ThreadModel +{ + Single, + Posix, + Win32, +} + +enum ThreadModel GNU_Thread_Model = ThreadModel.@DCFG_THREAD_MODEL@; + +// Whether the linker provides __start_minfo and __stop_minfo symbols +enum Minfo_Bracketing = @DCFG_MINFO_BRACKETING@; + +// Whether target has support for builtin atomics. +enum GNU_Have_Atomics = @DCFG_HAVE_ATOMIC_BUILTINS@; + +// Whether target has support for 64-bit builtin atomics. +enum GNU_Have_64Bit_Atomics = @DCFG_HAVE_64BIT_ATOMICS@; + +// Do we have libatomic available +enum GNU_Have_LibAtomic = @DCFG_HAVE_LIBATOMIC@; diff --git a/libphobos/libdruntime/gcc/deh.d b/libphobos/libdruntime/gcc/deh.d new file mode 100644 index 0000000..8323f4a --- /dev/null +++ b/libphobos/libdruntime/gcc/deh.d @@ -0,0 +1,991 @@ +// GNU D Compiler exception personality routines. +// Copyright (C) 2011-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +// This code is based on the libstdc++ exception handling routines. + +module gcc.deh; + +import gcc.unwind; +import gcc.unwind.pe; +import gcc.builtins; +import gcc.config; + +extern(C) +{ + int _d_isbaseof(ClassInfo, ClassInfo); + void _d_createTrace(Object, void*); + + // Not used in GDC but declaration required by rt/sections.d + struct FuncTable + { + } +} + +/** + * Declare all known and handled exception classes. + * D exceptions -- "GNUCD\0\0\0". + * C++ exceptions -- "GNUCC++\0" + * C++ dependent exceptions -- "GNUCC++\x01" + */ +static if (GNU_ARM_EABI_Unwinder) +{ + enum _Unwind_Exception_Class gdcExceptionClass = "GNUCD\0\0\0"; + enum _Unwind_Exception_Class gxxExceptionClass = "GNUCC++\0"; + enum _Unwind_Exception_Class gxxDependentExceptionClass = "GNUCC++\x01"; +} +else +{ + enum _Unwind_Exception_Class gdcExceptionClass = + (cast(_Unwind_Exception_Class)'G' << 56) | + (cast(_Unwind_Exception_Class)'N' << 48) | + (cast(_Unwind_Exception_Class)'U' << 40) | + (cast(_Unwind_Exception_Class)'C' << 32) | + (cast(_Unwind_Exception_Class)'D' << 24); + + enum _Unwind_Exception_Class gxxExceptionClass = + (cast(_Unwind_Exception_Class)'G' << 56) | + (cast(_Unwind_Exception_Class)'N' << 48) | + (cast(_Unwind_Exception_Class)'U' << 40) | + (cast(_Unwind_Exception_Class)'C' << 32) | + (cast(_Unwind_Exception_Class)'C' << 24) | + (cast(_Unwind_Exception_Class)'+' << 16) | + (cast(_Unwind_Exception_Class)'+' << 8) | + (cast(_Unwind_Exception_Class)0 << 0); + + enum _Unwind_Exception_Class gxxDependentExceptionClass = + gxxExceptionClass + 1; +} + +/** + * Checks for GDC exception class. + */ +bool isGdcExceptionClass(_Unwind_Exception_Class c) @nogc +{ + static if (GNU_ARM_EABI_Unwinder) + { + return c[0] == gdcExceptionClass[0] + && c[1] == gdcExceptionClass[1] + && c[2] == gdcExceptionClass[2] + && c[3] == gdcExceptionClass[3] + && c[4] == gdcExceptionClass[4] + && c[5] == gdcExceptionClass[5] + && c[6] == gdcExceptionClass[6] + && c[7] == gdcExceptionClass[7]; + } + else + { + return c == gdcExceptionClass; + } +} + +/** + * Checks for any C++ exception class. + */ +bool isGxxExceptionClass(_Unwind_Exception_Class c) @nogc +{ + static if (GNU_ARM_EABI_Unwinder) + { + return c[0] == gxxExceptionClass[0] + && c[1] == gxxExceptionClass[1] + && c[2] == gxxExceptionClass[2] + && c[3] == gxxExceptionClass[3] + && c[4] == gxxExceptionClass[4] + && c[5] == gxxExceptionClass[5] + && c[6] == gxxExceptionClass[6] + && (c[7] == gxxExceptionClass[7] + || c[7] == gxxDependentExceptionClass[7]); + } + else + { + return c == gxxExceptionClass + || c == gxxDependentExceptionClass; + } +} + +/** + * Checks for primary or dependent, but not that it is a C++ exception. + */ +bool isDependentException(_Unwind_Exception_Class c) @nogc +{ + static if (GNU_ARM_EABI_Unwinder) + return (c[7] == '\x01'); + else + return (c & 1); +} + +/** + * A D exception object consists of a header, which is a wrapper + * around an unwind object header with additional D specific + * information, prefixed by the exception object itself. + */ +struct ExceptionHeader +{ + // Because of a lack of __aligned__ style attribute, our object + // and the unwind object are the first two fields. + static if (Throwable.alignof < _Unwind_Exception.alignof) + ubyte[_Unwind_Exception.alignof - Throwable.alignof] pad; + + // The object being thrown. The compiled code expects this to + // be immediately before the generic exception header. + Throwable object; + + // The generic exception header. + _Unwind_Exception unwindHeader; + + static assert(unwindHeader.offsetof - object.offsetof == object.sizeof); + + // Cache handler details between Phase 1 and Phase 2. + static if (GNU_ARM_EABI_Unwinder) + { + // Nothing here yet. + } + else + { + // Which catch was found. + int handler; + + // Language Specific Data Area for function enclosing the handler. + const(ubyte)* languageSpecificData; + + // Pointer to catch code. + _Unwind_Ptr landingPad; + + // Canonical Frame Address (CFA) for the enclosing handler. + _Unwind_Word canonicalFrameAddress; + } + + // Stack other thrown exceptions in current thread through here. + ExceptionHeader* next; + + // Thread local stack of chained exceptions. + static ExceptionHeader* stack; + + // Pre-allocate storage for 1 instance per thread. + // Use calloc/free for multiple exceptions in flight. + static ExceptionHeader ehstorage; + + /** + * Allocate and initialize an ExceptionHeader. + */ + static ExceptionHeader* create(Throwable o) @nogc + { + auto eh = &ehstorage; + + // Check exception object in use. + if (eh.object) + { + eh = cast(ExceptionHeader*) __builtin_calloc(ExceptionHeader.sizeof, 1); + // Out of memory while throwing - not much else can be done. + if (!eh) + terminate("out of memory", __LINE__); + } + eh.object = o; + + eh.unwindHeader.exception_class = gdcExceptionClass; + + return eh; + } + + /** + * Free ExceptionHeader that was created by create(). + */ + static void free(ExceptionHeader* eh) @nogc + { + *eh = ExceptionHeader.init; + if (eh != &ehstorage) + __builtin_free(eh); + } + + /** + * Push this onto stack of chained exceptions. + */ + void push() @nogc + { + next = stack; + stack = &this; + } + + /** + * Pop and return top of chained exception stack. + */ + static ExceptionHeader* pop() @nogc + { + auto eh = stack; + stack = eh.next; + return eh; + } + + /** + * Save stage1 handler information in the exception object. + */ + static void save(_Unwind_Exception* unwindHeader, + _Unwind_Word cfa, int handler, + const(ubyte)* lsda, _Unwind_Ptr landingPad) @nogc + { + static if (GNU_ARM_EABI_Unwinder) + { + unwindHeader.barrier_cache.sp = cfa; + unwindHeader.barrier_cache.bitpattern[1] = cast(_uw)handler; + unwindHeader.barrier_cache.bitpattern[2] = cast(_uw)lsda; + unwindHeader.barrier_cache.bitpattern[3] = cast(_uw)landingPad; + } + else + { + ExceptionHeader* eh = toExceptionHeader(unwindHeader); + eh.canonicalFrameAddress = cfa; + eh.handler = handler; + eh.languageSpecificData = lsda; + eh.landingPad = landingPad; + } + } + + /** + * Restore the catch handler data saved during phase1. + */ + static void restore(_Unwind_Exception* unwindHeader, out int handler, + out const(ubyte)* lsda, out _Unwind_Ptr landingPad, + out _Unwind_Word cfa) @nogc + { + static if (GNU_ARM_EABI_Unwinder) + { + cfa = unwindHeader.barrier_cache.sp; + handler = cast(int)unwindHeader.barrier_cache.bitpattern[1]; + lsda = cast(ubyte*)unwindHeader.barrier_cache.bitpattern[2]; + landingPad = cast(_Unwind_Ptr)unwindHeader.barrier_cache.bitpattern[3]; + } + else + { + ExceptionHeader* eh = toExceptionHeader(unwindHeader); + cfa = eh.canonicalFrameAddress; + handler = eh.handler; + lsda = eh.languageSpecificData; + landingPad = cast(_Unwind_Ptr)eh.landingPad; + } + } + + /** + * Look at the chain of inflight exceptions and pick the class type that'll + * be looked for in catch clauses. + */ + static ClassInfo getClassInfo(_Unwind_Exception* unwindHeader) @nogc + { + ExceptionHeader* eh = toExceptionHeader(unwindHeader); + // The first thrown Exception at the top of the stack takes precedence + // over others that are inflight, unless an Error was thrown, in which + // case, we search for error handlers instead. + Throwable ehobject = eh.object; + for (ExceptionHeader* ehn = eh.next; ehn; ehn = ehn.next) + { + Error e = cast(Error)ehobject; + if (e is null || (cast(Error)ehn.object) !is null) + ehobject = ehn.object; + } + return ehobject.classinfo; + } + + /** + * Convert from pointer to unwindHeader to pointer to ExceptionHeader + * that it is embedded inside of. + */ + static ExceptionHeader* toExceptionHeader(_Unwind_Exception* exc) @nogc + { + return cast(ExceptionHeader*)(cast(void*)exc - ExceptionHeader.unwindHeader.offsetof); + } +} + +/** + * Map to C++ std::type_info's virtual functions from D, + * being careful to not require linking with libstdc++. + * So it is given a different name. + */ +extern(C++) interface CxxTypeInfo +{ + void dtor1(); + void dtor2(); + bool __is_pointer_p() const; + bool __is_function_p() const; + bool __do_catch(const CxxTypeInfo, void**, uint) const; + bool __do_upcast(const void*, void**) const; +} + +/** + * Structure of a C++ exception, represented as a C structure. + * See unwind-cxx.h for the full definition. + */ +struct CxaExceptionHeader +{ + union + { + CxxTypeInfo exceptionType; + void* primaryException; + } + void function(void*) exceptionDestructor; + void function() unexpectedHandler; + void function() terminateHandler; + CxaExceptionHeader* nextException; + int handlerCount; + + static if (GNU_ARM_EABI_Unwinder) + { + CxaExceptionHeader* nextPropagatingException; + int propagationCount; + } + else + { + int handlerSwitchValue; + const(ubyte)* actionRecord; + const(ubyte)* languageSpecificData; + _Unwind_Ptr catchTemp; + void* adjustedPtr; + } + + _Unwind_Exception unwindHeader; + + /** + * There's no saving between phases, so only cache pointer. + * __cxa_begin_catch expects this to be set. + */ + static void save(_Unwind_Exception* unwindHeader, void* thrownPtr) @nogc + { + static if (GNU_ARM_EABI_Unwinder) + unwindHeader.barrier_cache.bitpattern[0] = cast(_uw) thrownPtr; + else + { + auto eh = toExceptionHeader(unwindHeader); + eh.adjustedPtr = thrownPtr; + } + } + + /** + * Get pointer to the thrown object if the thrown object type behind the + * exception is implicitly convertible to the catch type. + */ + static void* getAdjustedPtr(_Unwind_Exception* exc, CxxTypeInfo catchType) + { + void* thrownPtr; + + // A dependent C++ exceptions is just a wrapper around the unwind header. + // A primary C++ exception has the thrown object located immediately after it. + if (isDependentException(exc.exception_class)) + thrownPtr = toExceptionHeader(exc).primaryException; + else + thrownPtr = cast(void*)(exc + 1); + + // Pointer types need to adjust the actual pointer, not the pointer that is + // the exception object. This also has the effect of passing pointer types + // "by value" through the __cxa_begin_catch return value. + const throw_type = (cast(CxaExceptionHeader*)thrownPtr - 1).exceptionType; + + if (throw_type.__is_pointer_p()) + thrownPtr = *cast(void**)thrownPtr; + + // Pointer adjustment may be necessary due to multiple inheritance + if (catchType is throw_type + || catchType.__do_catch(throw_type, &thrownPtr, 1)) + return thrownPtr; + + return null; + } + + /** + * Convert from pointer to unwindHeader to pointer to CxaExceptionHeader + * that it is embedded inside of. + */ + static CxaExceptionHeader* toExceptionHeader(_Unwind_Exception* exc) @nogc + { + return cast(CxaExceptionHeader*)(exc + 1) - 1; + } +} + +/** + * Called if exception handling must be abandoned for any reason. + */ +private void terminate(string msg, uint line) @nogc +{ + import core.stdc.stdio; + import core.stdc.stdlib; + + static bool terminating; + if (terminating) + { + fputs("terminate called recursively\n", stderr); + abort(); + } + terminating = true; + + fprintf(stderr, "gcc.deh(%u): %.*s\n", line, cast(int)msg.length, msg.ptr); + + abort(); +} + +/** + * Called when fibers switch contexts. + */ +extern(C) void* _d_eh_swapContext(void* newContext) nothrow @nogc +{ + auto old = ExceptionHeader.stack; + ExceptionHeader.stack = cast(ExceptionHeader*)newContext; + return old; +} + +/** + * Called before starting a catch. Returns the exception object. + */ +extern(C) void* __gdc_begin_catch(_Unwind_Exception* unwindHeader) +{ + ExceptionHeader* header = ExceptionHeader.toExceptionHeader(unwindHeader); + + void* objectp = cast(void*)header.object; + + // Something went wrong when stacking up chained headers... + if (header != ExceptionHeader.pop()) + terminate("catch error", __LINE__); + + // Handling for this exception is complete. + _Unwind_DeleteException(&header.unwindHeader); + + return objectp; +} + +/** + * Perform a throw, D style. Throw will unwind through this call, + * so there better not be any handlers or exception thrown here. + */ +extern(C) void _d_throw(Throwable object) +{ + // If possible, avoid always allocating new memory for exception headers. + ExceptionHeader *eh = ExceptionHeader.create(object); + + // Add to thrown exception stack. + eh.push(); + + // Called by unwinder when exception object needs destruction by other than our code. + extern(C) void exception_cleanup(_Unwind_Reason_Code code, _Unwind_Exception* exc) + { + // If we haven't been caught by a foreign handler, then this is + // some sort of unwind error. In that case just die immediately. + // _Unwind_DeleteException in the HP-UX IA64 libunwind library + // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT + // like the GCC _Unwind_DeleteException function does. + if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON) + terminate("uncaught exception", __LINE__); + + auto eh = ExceptionHeader.toExceptionHeader(exc); + ExceptionHeader.free(eh); + } + + eh.unwindHeader.exception_cleanup = &exception_cleanup; + + // Runtime now expects us to do this first before unwinding. + _d_createTrace(eh.object, null); + + // We're happy with setjmp/longjmp exceptions or region-based + // exception handlers: entry points are provided here for both. + _Unwind_Reason_Code r = void; + + version (GNU_SjLj_Exceptions) + r = _Unwind_SjLj_RaiseException(&eh.unwindHeader); + else + r = _Unwind_RaiseException(&eh.unwindHeader); + + // If code == _URC_END_OF_STACK, then we reached top of stack without finding + // a handler for the exception. Since each thread is run in a try/catch, + // this oughtn't happen. If code is something else, we encountered some sort + // of heinous lossage from which we could not recover. As is the way of such + // things, almost certainly we will have crashed before now, rather than + // actually being able to diagnose the problem. + if (r == _URC_END_OF_STACK) + terminate("uncaught exception", __LINE__); + + terminate("unwind error", __LINE__); +} + + +/** + * Read and extract information from the LSDA (.gcc_except_table section). + */ +_Unwind_Reason_Code scanLSDA(const(ubyte)* lsda, _Unwind_Exception_Class exceptionClass, + _Unwind_Action actions, _Unwind_Exception* unwindHeader, + _Unwind_Context* context, _Unwind_Word cfa, + out _Unwind_Ptr landingPad, out int handler) +{ + // If no LSDA, then there are no handlers or cleanups. + if (lsda is null) + return CONTINUE_UNWINDING(unwindHeader, context); + + // Parse the LSDA header + auto p = lsda; + + auto Start = (context ? _Unwind_GetRegionStart(context) : 0); + + // Find @LPStart, the base to which landing pad offsets are relative. + ubyte LPStartEncoding = *p++; + _Unwind_Ptr LPStart = 0; + + if (LPStartEncoding != DW_EH_PE_omit) + LPStart = read_encoded_value(context, LPStartEncoding, &p); + else + LPStart = Start; + + // Find @TType, the base of the handler and exception spec type data. + ubyte TTypeEncoding = *p++; + const(ubyte)* TType = null; + + if (TTypeEncoding != DW_EH_PE_omit) + { + static if (__traits(compiles, _TTYPE_ENCODING)) + { + // Older ARM EABI toolchains set this value incorrectly, so use a + // hardcoded OS-specific format. + TTypeEncoding = _TTYPE_ENCODING; + } + auto TTbase = read_uleb128(&p); + TType = p + TTbase; + } + + // The encoding and length of the call-site table; the action table + // immediately follows. + ubyte CSEncoding = *p++; + auto CSTableSize = read_uleb128(&p); + const(ubyte)* actionTable = p + CSTableSize; + + auto TTypeBase = base_of_encoded_value(TTypeEncoding, context); + + // Get instruction pointer (ip) at start of instruction that threw. + version (CRuntime_Glibc) + { + int ip_before_insn; + auto ip = _Unwind_GetIPInfo(context, &ip_before_insn); + if (!ip_before_insn) + --ip; + } + else + { + auto ip = _Unwind_GetIP(context); + --ip; + } + + bool saw_cleanup = false; + bool saw_handler = false; + const(ubyte)* actionRecord = null; + + version (GNU_SjLj_Exceptions) + { + // The given "IP" is an index into the call-site table, with two + // exceptions -- -1 means no-action, and 0 means terminate. + // But since we're using uleb128 values, we've not got random + // access to the array. + if (cast(int) ip <= 0) + { + return _URC_CONTINUE_UNWIND; + } + else + { + _uleb128_t CSLandingPad, CSAction; + do + { + CSLandingPad = read_uleb128(&p); + CSAction = read_uleb128(&p); + } + while (--ip); + + // Can never have null landing pad for sjlj -- that would have + // been indicated by a -1 call site index. + landingPad = CSLandingPad + 1; + if (CSAction) + actionRecord = actionTable + CSAction - 1; + } + } + else + { + // Search the call-site table for the action associated with this IP. + while (p < actionTable) + { + // Note that all call-site encodings are "absolute" displacements. + auto CSStart = read_encoded_value(null, CSEncoding, &p); + auto CSLen = read_encoded_value(null, CSEncoding, &p); + auto CSLandingPad = read_encoded_value(null, CSEncoding, &p); + auto CSAction = read_uleb128(&p); + + // The table is sorted, so if we've passed the ip, stop. + if (ip < Start + CSStart) + p = actionTable; + else if (ip < Start + CSStart + CSLen) + { + if (CSLandingPad) + landingPad = LPStart + CSLandingPad; + if (CSAction) + actionRecord = actionTable + CSAction - 1; + break; + } + } + } + + if (landingPad == 0) + { + // IP is present, but has a null landing pad. + // No cleanups or handlers to be run. + } + else if (actionRecord is null) + { + // If ip is present, has a non-null landing pad, and a null + // action table offset, then there are only cleanups present. + // Cleanups use a zero switch value, as set above. + saw_cleanup = true; + } + else + { + // Otherwise we have a catch handler or exception specification. + handler = actionTableLookup(actions, unwindHeader, actionRecord, + exceptionClass, TTypeBase, + TType, TTypeEncoding, + saw_handler, saw_cleanup); + } + + // IP is not in table. No associated cleanups. + if (!saw_handler && !saw_cleanup) + return CONTINUE_UNWINDING(unwindHeader, context); + + if (actions & _UA_SEARCH_PHASE) + { + if (!saw_handler) + return CONTINUE_UNWINDING(unwindHeader, context); + + // For domestic exceptions, we cache data from phase 1 for phase 2. + if (isGdcExceptionClass(exceptionClass)) + ExceptionHeader.save(unwindHeader, cfa, handler, lsda, landingPad); + + return _URC_HANDLER_FOUND; + } + + return 0; +} + +/** + * Look up and return the handler index of the classType in Action Table. + */ +int actionTableLookup(_Unwind_Action actions, _Unwind_Exception* unwindHeader, + const(ubyte)* actionRecord, _Unwind_Exception_Class exceptionClass, + _Unwind_Ptr TTypeBase, const(ubyte)* TType, + ubyte TTypeEncoding, + out bool saw_handler, out bool saw_cleanup) +{ + ClassInfo thrownType; + if (isGdcExceptionClass(exceptionClass)) + { + thrownType = ExceptionHeader.getClassInfo(unwindHeader); + } + + while (1) + { + auto ap = actionRecord; + auto ARFilter = read_sleb128(&ap); + auto apn = ap; + auto ARDisp = read_sleb128(&ap); + + if (ARFilter == 0) + { + // Zero filter values are cleanups. + saw_cleanup = true; + } + else if (actions & _UA_FORCE_UNWIND) + { + // During forced unwinding, we only run cleanups. + } + else if (ARFilter > 0) + { + // Positive filter values are handlers. + auto encodedSize = size_of_encoded_value(TTypeEncoding); + + // ARFilter is the negative index from TType, which is where + // the ClassInfo is stored. + const(ubyte)* tp = TType - ARFilter * encodedSize; + + auto entry = read_encoded_value_with_base(TTypeEncoding, TTypeBase, &tp); + ClassInfo ci = cast(ClassInfo)cast(void*)(entry); + + // D does not have catch-all handlers, and so the following + // assumes that we will never handle a null value. + assert(ci !is null); + + if (ci.classinfo is __cpp_type_info_ptr.classinfo + && isGxxExceptionClass(exceptionClass)) + { + // catchType is the catch clause type_info. + auto catchType = cast(CxxTypeInfo)((cast(__cpp_type_info_ptr)cast(void*)ci).ptr); + auto thrownPtr = CxaExceptionHeader.getAdjustedPtr(unwindHeader, catchType); + + if (thrownPtr !is null) + { + if (actions & _UA_SEARCH_PHASE) + CxaExceptionHeader.save(unwindHeader, thrownPtr); + saw_handler = true; + return cast(int)ARFilter; + } + } + else if (isGdcExceptionClass(exceptionClass) + && _d_isbaseof(thrownType, ci)) + { + saw_handler = true; + return cast(int)ARFilter; + } + else + { + // ??? What to do about other GNU language exceptions. + } + } + else + { + // Negative filter values are exception specifications, + // which D does not use. + break; + } + + if (ARDisp == 0) + break; + actionRecord = apn + ARDisp; + } + + return 0; +} + +/** + * Called when the personality function has found neither a cleanup or handler. + * To support ARM EABI personality routines, that must also unwind the stack. + */ +_Unwind_Reason_Code CONTINUE_UNWINDING(_Unwind_Exception* unwindHeader, _Unwind_Context* context) +{ + static if (GNU_ARM_EABI_Unwinder) + { + if (__gnu_unwind_frame(unwindHeader, context) != _URC_OK) + return _URC_FAILURE; + } + return _URC_CONTINUE_UNWIND; +} + +/** + * Using a different personality function name causes link failures + * when trying to mix code using different exception handling models. + */ +version (GNU_SEH_Exceptions) +{ + enum PERSONALITY_FUNCTION = "__gdc_personality_imp"; + + extern(C) EXCEPTION_DISPOSITION __gdc_personality_seh0(void* ms_exc, void* this_frame, + void* ms_orig_context, void* ms_disp) + { + return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, + ms_disp, &__gdc_personality_imp); + } +} +else version (GNU_SjLj_Exceptions) +{ + enum PERSONALITY_FUNCTION = "__gdc_personality_sj0"; + + private int __builtin_eh_return_data_regno(int x) { return x; } +} +else +{ + enum PERSONALITY_FUNCTION = "__gdc_personality_v0"; +} + +/** + * The "personality" function, specific to each language. + */ +static if (GNU_ARM_EABI_Unwinder) +{ + pragma(mangle, PERSONALITY_FUNCTION) + extern(C) _Unwind_Reason_Code gdc_personality(_Unwind_State state, + _Unwind_Exception* unwindHeader, + _Unwind_Context* context) + { + _Unwind_Action actions; + + switch (state & _US_ACTION_MASK) + { + case _US_VIRTUAL_UNWIND_FRAME: + // If the unwind state pattern is (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND) + // then we don't need to search for any handler as it is not a real exception. + // Just unwind the stack. + if (state & _US_FORCE_UNWIND) + return CONTINUE_UNWINDING(unwindHeader, context); + actions = _UA_SEARCH_PHASE; + break; + + case _US_UNWIND_FRAME_STARTING: + actions = _UA_CLEANUP_PHASE; + if (!(state & _US_FORCE_UNWIND) + && unwindHeader.barrier_cache.sp == _Unwind_GetGR(context, UNWIND_STACK_REG)) + actions |= _UA_HANDLER_FRAME; + break; + + case _US_UNWIND_FRAME_RESUME: + return CONTINUE_UNWINDING(unwindHeader, context); + + default: + terminate("unwind error", __LINE__); + } + actions |= state & _US_FORCE_UNWIND; + + // The dwarf unwinder assumes the context structure holds things like + // the function and LSDA pointers. The ARM implementation caches these + // in the exception header (UCB). To avoid rewriting everything we make + // the virtual IP register point at the UCB. + _Unwind_SetGR(context, UNWIND_POINTER_REG, cast(_Unwind_Ptr)unwindHeader); + + return __gdc_personality(actions, unwindHeader.exception_class, + unwindHeader, context); + } +} +else +{ + pragma(mangle, PERSONALITY_FUNCTION) + extern(C) _Unwind_Reason_Code gdc_personality(int iversion, + _Unwind_Action actions, + _Unwind_Exception_Class exceptionClass, + _Unwind_Exception* unwindHeader, + _Unwind_Context* context) + { + // Interface version check. + if (iversion != 1) + return _URC_FATAL_PHASE1_ERROR; + + return __gdc_personality(actions, exceptionClass, unwindHeader, context); + } +} + +private _Unwind_Reason_Code __gdc_personality(_Unwind_Action actions, + _Unwind_Exception_Class exceptionClass, + _Unwind_Exception* unwindHeader, + _Unwind_Context* context) +{ + const(ubyte)* lsda; + _Unwind_Ptr landingPad; + _Unwind_Word cfa; + int handler; + + // Shortcut for phase 2 found handler for domestic exception. + if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME) + && isGdcExceptionClass(exceptionClass)) + { + ExceptionHeader.restore(unwindHeader, handler, lsda, landingPad, cfa); + // Shouldn't have cached a null landing pad in phase 1. + if (landingPad == 0) + terminate("unwind error", __LINE__); + } + else + { + lsda = cast(ubyte*)_Unwind_GetLanguageSpecificData(context); + + static if (GNU_ARM_EABI_Unwinder) + cfa = _Unwind_GetGR(context, UNWIND_STACK_REG); + else + cfa = _Unwind_GetCFA(context); + + auto result = scanLSDA(lsda, exceptionClass, actions, unwindHeader, + context, cfa, landingPad, handler); + + // Positive on handler found in phase 1, continue unwinding, or failure. + if (result) + return result; + } + + // Unexpected negative handler, call terminate directly. + if (handler < 0) + terminate("unwind error", __LINE__); + + // We can't use any of the deh routines with foreign exceptions, + // because they all expect unwindHeader to be an ExceptionHeader. + if (isGdcExceptionClass(exceptionClass)) + { + // If there are any in-flight exceptions being thrown, chain our + // current object onto the end of the prevous object. + ExceptionHeader* eh = ExceptionHeader.toExceptionHeader(unwindHeader); + auto currentLsd = lsda; + auto currentCfa = cfa; + bool bypassed = false; + + while (eh.next) + { + ExceptionHeader* ehn = eh.next; + const(ubyte)* nextLsd; + _Unwind_Ptr nextLandingPad; + _Unwind_Word nextCfa; + int nextHandler; + + ExceptionHeader.restore(&ehn.unwindHeader, nextHandler, nextLsd, nextLandingPad, nextCfa); + + Error e = cast(Error)eh.object; + if (e !is null && !cast(Error)ehn.object) + { + // We found an Error, bypass the exception chain. + currentLsd = nextLsd; + currentCfa = nextCfa; + eh = ehn; + bypassed = true; + continue; + } + + // Don't combine when the exceptions are from different functions. + if (currentLsd != nextLsd && currentCfa != nextCfa) + break; + + // Add our object onto the end of the existing chain. + Throwable n = ehn.object; + while (n.next) + n = n.next; + n.next = eh.object; + + // Replace our exception object with in-flight one + eh.object = ehn.object; + if (nextHandler != handler && !bypassed) + { + handler = nextHandler; + ExceptionHeader.save(unwindHeader, cfa, handler, lsda, landingPad); + } + + // Exceptions chained, can now throw away the previous header. + eh.next = ehn.next; + _Unwind_DeleteException(&ehn.unwindHeader); + } + + if (bypassed) + { + eh = ExceptionHeader.toExceptionHeader(unwindHeader); + Error e = cast(Error)eh.object; + auto ehn = eh.next; + e.bypassedException = ehn.object; + eh.next = ehn.next; + _Unwind_DeleteException(&ehn.unwindHeader); + } + } + + // Set up registers and jump to cleanup or handler. + // For targets with pointers smaller than the word size, we must extend the + // pointer, and this extension is target dependent. + _Unwind_SetGR(context, __builtin_eh_return_data_regno(0), + cast(_Unwind_Ptr)unwindHeader); + _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), handler); + _Unwind_SetIP(context, landingPad); + + return _URC_INSTALL_CONTEXT; +} diff --git a/libphobos/libdruntime/gcc/libbacktrace.d.in b/libphobos/libdruntime/gcc/libbacktrace.d.in new file mode 100644 index 0000000..7602387 --- /dev/null +++ b/libphobos/libdruntime/gcc/libbacktrace.d.in @@ -0,0 +1,73 @@ +// GNU D Compiler bindings for the stack backtrace functions. +// Copyright (C) 2013-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +module gcc.libbacktrace; + +/* + * Part of backtrace-supported.h: These are platform specific variables. + * They are obtained via the configure script + */ + +enum BACKTRACE_SUPPORTED = @BACKTRACE_SUPPORTED@; +enum BACKTRACE_USES_MALLOC = @BACKTRACE_USES_MALLOC@; +enum BACKTRACE_SUPPORTS_THREADS = @BACKTRACE_SUPPORTS_THREADS@; + +/* + * libbacktrace.h + */ + +static if (BACKTRACE_SUPPORTED) +{ + import core.stdc.stddef, core.stdc.stdio, core.stdc.stdint; + +extern(C): + struct backtrace_state {} + + alias extern(C) void function(void* data, const(char)* msg, int errnum) + backtrace_error_callback; + + backtrace_state* backtrace_create_state(const(char)* filename, int threaded, + backtrace_error_callback error_callback, void* data) nothrow; + + alias extern(C) int function(void* data, uintptr_t pc, const(char)* filename, int lineno, const(char)* func) + backtrace_full_callback; + + int backtrace_full(backtrace_state* state, int skip, backtrace_full_callback callback, + backtrace_error_callback error_callback, void* data) nothrow; + + alias extern(C) int function(void* data, uintptr_t pc) + backtrace_simple_callback; + + int backtrace_simple(backtrace_state* state, int skip, backtrace_simple_callback callback, + backtrace_error_callback error_callback, void* data) nothrow; + + void backtrace_print(backtrace_state* state, int skip, FILE* file) nothrow; + + int backtrace_pcinfo(backtrace_state* state, uintptr_t pc, backtrace_full_callback callback, + backtrace_error_callback error_callback, void* data) nothrow; + + alias extern(C) void function(void* data, uintptr_t pc, const(char)* symname, uintptr_t symval) + backtrace_syminfo_callback; + + int backtrace_syminfo(backtrace_state *state, uintptr_t pc, backtrace_syminfo_callback callback, + backtrace_error_callback error_callback, void* data) nothrow; +} diff --git a/libphobos/libdruntime/gcc/unwind/arm.d b/libphobos/libdruntime/gcc/unwind/arm.d new file mode 100644 index 0000000..a69cd28 --- /dev/null +++ b/libphobos/libdruntime/gcc/unwind/arm.d @@ -0,0 +1,71 @@ +// Exception handling and frame unwind runtime interface routines. +// Copyright (C) 2011-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +// extern(C) interface for the ARM EABI unwinder library. +// This corresponds to unwind-arm.h + +module gcc.unwind.arm; + +import gcc.config; + +version (ARM): +static if (GNU_ARM_EABI_Unwinder): + +public import gcc.unwind.arm_common; +import gcc.unwind.pe; + +extern (C): +@nogc: + +enum int UNWIND_STACK_REG = 13; +// Use IP as a scratch register within the personality routine. +enum int UNWIND_POINTER_REG = 12; + +version (linux) + enum _TTYPE_ENCODING = (DW_EH_PE_pcrel | DW_EH_PE_indirect); +else version (NetBSD) + enum _TTYPE_ENCODING = (DW_EH_PE_pcrel | DW_EH_PE_indirect); +else version (FreeBSD) + enum _TTYPE_ENCODING = (DW_EH_PE_pcrel | DW_EH_PE_indirect); +else version (Symbian) + enum _TTYPE_ENCODING = (DW_EH_PE_absptr); +else version (uClinux) + enum _TTYPE_ENCODING = (DW_EH_PE_absptr); +else + enum _TTYPE_ENCODING = (DW_EH_PE_pcrel); + +// Return the address of the instruction, not the actual IP value. +_Unwind_Word _Unwind_GetIP(_Unwind_Context* context) +{ + return _Unwind_GetGR(context, 15) & ~ cast(_Unwind_Word) 1; +} + +void _Unwind_SetIP(_Unwind_Context* context, _Unwind_Word val) +{ + return _Unwind_SetGR(context, 15, val | (_Unwind_GetGR(context, 15) & 1)); +} + +_Unwind_Word _Unwind_GetIPInfo(_Unwind_Context* context, int* ip_before_insn) +{ + *ip_before_insn = 0; + return _Unwind_GetIP(context); +} diff --git a/libphobos/libdruntime/gcc/unwind/arm_common.d b/libphobos/libdruntime/gcc/unwind/arm_common.d new file mode 100644 index 0000000..a756a49 --- /dev/null +++ b/libphobos/libdruntime/gcc/unwind/arm_common.d @@ -0,0 +1,244 @@ +// Exception handling and frame unwind runtime interface routines. +// Copyright (C) 2011-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +// extern(C) interface for the ARM EABI and C6X unwinders. +// This corresponds to unwind-arm-common.h + +module gcc.unwind.arm_common; + +import gcc.config; + +static if (GNU_ARM_EABI_Unwinder): + +import gcc.builtins; + +extern (C): + +// Placed outside @nogc in order to not constrain what the callback does. +// ??? Does this really need to be extern(C) alias? +extern(C) alias _Unwind_Exception_Cleanup_Fn + = void function(_Unwind_Reason_Code, _Unwind_Exception*); + +extern(C) alias personality_routine + = _Unwind_Reason_Code function(_Unwind_State, + _Unwind_Control_Block*, + _Unwind_Context*); + +extern(C) alias _Unwind_Stop_Fn + =_Unwind_Reason_Code function(int, _Unwind_Action, + _Unwind_Exception_Class, + _Unwind_Control_Block*, + _Unwind_Context*, void*); + +extern(C) alias _Unwind_Trace_Fn + = _Unwind_Reason_Code function(_Unwind_Context*, void*); + +@nogc: + +alias _Unwind_Word = __builtin_machine_uint; +alias _Unwind_Sword = __builtin_machine_int; +alias _Unwind_Ptr = __builtin_pointer_uint; +alias _Unwind_Internal_Ptr =__builtin_pointer_uint; +alias _uw = _Unwind_Word; +alias _uw64 = ulong; +alias _uw16 = ushort; +alias _uw8 = ubyte; + +alias _Unwind_Reason_Code = uint; +enum : _Unwind_Reason_Code +{ + _URC_OK = 0, // operation completed successfully + _URC_FOREIGN_EXCEPTION_CAUGHT = 1, + _URC_END_OF_STACK = 5, + _URC_HANDLER_FOUND = 6, + _URC_INSTALL_CONTEXT = 7, + _URC_CONTINUE_UNWIND = 8, + _URC_FAILURE = 9 // unspecified failure of some kind +} + +alias _Unwind_State = int; +enum : _Unwind_State +{ + _US_VIRTUAL_UNWIND_FRAME = 0, + _US_UNWIND_FRAME_STARTING = 1, + _US_UNWIND_FRAME_RESUME = 2, + _US_ACTION_MASK = 3, + _US_FORCE_UNWIND = 8, + _US_END_OF_STACK = 16 +} + +// Provided only for for compatibility with existing code. +alias _Unwind_Action = int; +enum : _Unwind_Action +{ + _UA_SEARCH_PHASE = 1, + _UA_CLEANUP_PHASE = 2, + _UA_HANDLER_FRAME = 4, + _UA_FORCE_UNWIND = 8, + _UA_END_OF_STACK = 16, + _URC_NO_REASON = _URC_OK +} + +struct _Unwind_Context; +alias _Unwind_EHT_Header = _uw; + +struct _Unwind_Control_Block +{ + _Unwind_Exception_Class exception_class = '\0'; + _Unwind_Exception_Cleanup_Fn exception_cleanup; + // Unwinder cache, private fields for the unwinder's use + struct _unwinder_cache + { + _uw reserved1; // Forced unwind stop fn, 0 if not forced + _uw reserved2; // Personality routine address + _uw reserved3; // Saved callsite address + _uw reserved4; // Forced unwind stop arg + _uw reserved5; + } + _unwinder_cache unwinder_cache; + // Propagation barrier cache (valid after phase 1): + struct _barrier_cache + { + _uw sp; + _uw[5] bitpattern; + } + _barrier_cache barrier_cache; + // Cleanup cache (preserved over cleanup): + struct _cleanup_cache + { + _uw[4] bitpattern; + } + _cleanup_cache cleanup_cache; + // Pr cache (for pr's benefit): + struct _pr_cache + { + _uw fnstart; // function start address */ + _Unwind_EHT_Header* ehtp; // pointer to EHT entry header word + _uw additional; // additional data + _uw reserved1; + } + _pr_cache pr_cache; + long[0] _force_alignment; // Force alignment to 8-byte boundary +} + +// Virtual Register Set +alias _Unwind_VRS_RegClass = int; +enum : _Unwind_VRS_RegClass +{ + _UVRSC_CORE = 0, // integer register + _UVRSC_VFP = 1, // vfp + _UVRSC_FPA = 2, // fpa + _UVRSC_WMMXD = 3, // Intel WMMX data register + _UVRSC_WMMXC = 4 // Intel WMMX control register +} + +alias _Unwind_VRS_DataRepresentation = int; +enum : _Unwind_VRS_DataRepresentation +{ + _UVRSD_UINT32 = 0, + _UVRSD_VFPX = 1, + _UVRSD_FPAX = 2, + _UVRSD_UINT64 = 3, + _UVRSD_FLOAT = 4, + _UVRSD_DOUBLE = 5 +} + +alias _Unwind_VRS_Result = int; +enum : _Unwind_VRS_Result +{ + _UVRSR_OK = 0, + _UVRSR_NOT_IMPLEMENTED = 1, + _UVRSR_FAILED = 2 +} + +// Frame unwinding state. +struct __gnu_unwind_state +{ + _uw data; // The current word (bytes packed msb first). + _uw* next; // Pointer to the next word of data. + _uw8 bytes_left; // The number of bytes left in this word. + _uw8 words_left; // The number of words pointed to by ptr. +} + +_Unwind_VRS_Result _Unwind_VRS_Set(_Unwind_Context*, _Unwind_VRS_RegClass, + _uw, _Unwind_VRS_DataRepresentation, + void*); + +_Unwind_VRS_Result _Unwind_VRS_Get(_Unwind_Context*, _Unwind_VRS_RegClass, + _uw, _Unwind_VRS_DataRepresentation, + void*); + +_Unwind_VRS_Result _Unwind_VRS_Pop(_Unwind_Context*, _Unwind_VRS_RegClass, + _uw, _Unwind_VRS_DataRepresentation); + + +// Support functions for the PR. +alias _Unwind_Exception = _Unwind_Control_Block; +alias _Unwind_Exception_Class = char[8]; + +void* _Unwind_GetLanguageSpecificData(_Unwind_Context*); +_Unwind_Ptr _Unwind_GetRegionStart(_Unwind_Context*); + +_Unwind_Ptr _Unwind_GetDataRelBase(_Unwind_Context*); +// This should never be used. +_Unwind_Ptr _Unwind_GetTextRelBase(_Unwind_Context*); + +// Interface functions: +_Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Control_Block*); +void _Unwind_Resume(_Unwind_Control_Block*); +_Unwind_Reason_Code _Unwind_Resume_or_Rethrow(_Unwind_Control_Block*); + +_Unwind_Reason_Code _Unwind_ForcedUnwind(_Unwind_Control_Block*, + _Unwind_Stop_Fn, void*); + +// @@@ Use unwind data to perform a stack backtrace. The trace callback +// is called for every stack frame in the call chain, but no cleanup +// actions are performed. +_Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void*); + +_Unwind_Word _Unwind_GetCFA(_Unwind_Context*); +void _Unwind_Complete(_Unwind_Control_Block*); +void _Unwind_DeleteException(_Unwind_Exception*); + +_Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Control_Block*, + _Unwind_Context*); +_Unwind_Reason_Code __gnu_unwind_execute(_Unwind_Context*, + __gnu_unwind_state*); + +_Unwind_Word _Unwind_GetGR(_Unwind_Context* context, int regno) +{ + _uw val; + _Unwind_VRS_Get(context, _UVRSC_CORE, regno, _UVRSD_UINT32, &val); + return val; +} + +void _Unwind_SetGR(_Unwind_Context* context, int regno, _Unwind_Word val) +{ + _Unwind_VRS_Set(context, _UVRSC_CORE, regno, _UVRSD_UINT32, &val); +} + +// leb128 type numbers have a potentially unlimited size. +// The target of the following definitions of _sleb128_t and _uleb128_t +// is to have efficient data types large enough to hold the leb128 type +// numbers used in the unwind code. +alias _sleb128_t = __builtin_clong; +alias _uleb128_t = __builtin_culong; diff --git a/libphobos/libdruntime/gcc/unwind/c6x.d b/libphobos/libdruntime/gcc/unwind/c6x.d new file mode 100644 index 0000000..93a1944 --- /dev/null +++ b/libphobos/libdruntime/gcc/unwind/c6x.d @@ -0,0 +1,52 @@ +// Exception handling and frame unwind runtime interface routines. +// Copyright (C) 2011-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// extern(C) interface for the C6X EABI unwinder library. +// This corresponds to unwind-c6x.h + +module gcc.unwind.c6x; + +import gcc.config; + +version (TIC6X): +static if (GNU_ARM_EABI_Unwinder): + +// Not really the ARM EABI, but pretty close. +public import gcc.unwind.arm_common; + +extern (C): +@nogc: + +enum int UNWIND_STACK_REG = 31; +// Use A0 as a scratch register within the personality routine. +enum int UNWIND_POINTER_REG = 0; + +_Unwind_Word _Unwind_GetIP(_Unwind_Context* context) +{ + return _Unwind_GetGR(context, 33); +} + +void _Unwind_SetIP(_Unwind_Context* context, _Unwind_Word val) +{ + return _Unwind_SetGR(context, 33, val); +} + +_Unwind_Word _Unwind_GetIPInfo(_Unwind_Context* context, int* ip_before_insn) +{ + *ip_before_insn = 0; + return _Unwind_GetIP(context); +} diff --git a/libphobos/libdruntime/gcc/unwind/generic.d b/libphobos/libdruntime/gcc/unwind/generic.d new file mode 100644 index 0000000..e6071ba --- /dev/null +++ b/libphobos/libdruntime/gcc/unwind/generic.d @@ -0,0 +1,274 @@ +// Exception handling and frame unwind runtime interface routines. +// Copyright (C) 2011-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +// extern(C) interface for the generic unwinder library. +// This corresponds to unwind-generic.h + +module gcc.unwind.generic; + +import gcc.config; + +static if (!GNU_ARM_EABI_Unwinder): + +import gcc.builtins; + +// This is derived from the C++ ABI for IA-64. Where we diverge +// for cross-architecture compatibility are noted with "@@@". + +extern (C): + +// Placed outside @nogc in order to not constrain what the callback does. +// ??? Does this really need to be extern(C) alias? + +extern(C) alias _Unwind_Exception_Cleanup_Fn + = void function(_Unwind_Reason_Code, _Unwind_Exception*); + +extern(C) alias _Unwind_Stop_Fn + = _Unwind_Reason_Code function(int, _Unwind_Action, + _Unwind_Exception_Class, + _Unwind_Exception*, + _Unwind_Context*, void*); + +extern(C) alias _Unwind_Trace_Fn + = _Unwind_Reason_Code function(_Unwind_Context*, void*); + +// The personality routine is the function in the C++ (or other language) +// runtime library which serves as an interface between the system unwind +// library and language-specific exception handling semantics. It is +// specific to the code fragment described by an unwind info block, and +// it is always referenced via the pointer in the unwind info block, and +// hence it has no ABI-specified name. + +// Note that this implies that two different C++ implementations can +// use different names, and have different contents in the language +// specific data area. Moreover, that the language specific data +// area contains no version info because name of the function invoked +// provides more effective versioning by detecting at link time the +// lack of code to handle the different data format. + +extern(C) alias _Unwind_Personality_Fn + = _Unwind_Reason_Code function(int, _Unwind_Action, + _Unwind_Exception_Class, + _Unwind_Exception*, + _Unwind_Context*); + +@nogc: + +// Level 1: Base ABI + +// @@@ The IA-64 ABI uses uint64 throughout. +// Most places this is inefficient for 32-bit and smaller machines. +alias _Unwind_Word = __builtin_unwind_uint; +alias _Unwind_Sword = __builtin_unwind_int; +version (IA64) +{ + version (HPUX) + alias _Unwind_Ptr = __builtin_machine_uint; + else + alias _Unwind_Ptr = __builtin_pointer_uint; +} +else +{ + alias _Unwind_Ptr = __builtin_pointer_uint; +} +alias _Unwind_Internal_Ptr = __builtin_pointer_uint; + +// @@@ The IA-64 ABI uses a 64-bit word to identify the producer and +// consumer of an exception. We'll go along with this for now even on +// 32-bit machines. We'll need to provide some other option for +// 16-bit machines and for machines with > 8 bits per byte. +alias _Unwind_Exception_Class = ulong; + +// The unwind interface uses reason codes in several contexts to +// identify the reasons for failures or other actions. +alias _Unwind_Reason_Code = uint; +enum : _Unwind_Reason_Code +{ + _URC_NO_REASON = 0, + _URC_FOREIGN_EXCEPTION_CAUGHT = 1, + _URC_FATAL_PHASE2_ERROR = 2, + _URC_FATAL_PHASE1_ERROR = 3, + _URC_NORMAL_STOP = 4, + _URC_END_OF_STACK = 5, + _URC_HANDLER_FOUND = 6, + _URC_INSTALL_CONTEXT = 7, + _URC_CONTINUE_UNWIND = 8 +} + +// The unwind interface uses a pointer to an exception header object +// as its representation of an exception being thrown. In general, the +// full representation of an exception object is language- and +// implementation-specific, but it will be prefixed by a header +// understood by the unwind interface. + +// @@@ The IA-64 ABI says that this structure must be double-word aligned. +// Taking that literally does not make much sense generically. Instead we +// provide the maximum alignment required by any type for the machine. +struct _Unwind_Exception +{ + _Unwind_Exception_Class exception_class; + _Unwind_Exception_Cleanup_Fn exception_cleanup; + _Unwind_Word private_1; + _Unwind_Word private_2; +} + +// The ACTIONS argument to the personality routine is a bitwise OR of one +// or more of the following constants. +alias _Unwind_Action = int; +enum : _Unwind_Action +{ + _UA_SEARCH_PHASE = 1, + _UA_CLEANUP_PHASE = 2, + _UA_HANDLER_FRAME = 4, + _UA_FORCE_UNWIND = 8, + _UA_END_OF_STACK = 16 +} + +// This is an opaque type used to refer to a system-specific data +// structure used by the system unwinder. This context is created and +// destroyed by the system, and passed to the personality routine +// during unwinding. +struct _Unwind_Context; + +// Raise an exception, passing along the given exception object. +_Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception*); + +// Raise an exception for forced unwinding. +_Unwind_Reason_Code _Unwind_ForcedUnwind(_Unwind_Exception*, _Unwind_Stop_Fn, void*); + +// Helper to invoke the exception_cleanup routine. +void _Unwind_DeleteException(_Unwind_Exception*); + +// Resume propagation of an existing exception. This is used after +// e.g. executing cleanup code, and not to implement rethrowing. +void _Unwind_Resume(_Unwind_Exception*); + +// @@@ Resume propagation of an FORCE_UNWIND exception, or to rethrow +// a normal exception that was handled. +_Unwind_Reason_Code _Unwind_Resume_or_Rethrow(_Unwind_Exception*); + +// @@@ Use unwind data to perform a stack backtrace. The trace callback +// is called for every stack frame in the call chain, but no cleanup +// actions are performed. +_Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void*); + +// These functions are used for communicating information about the unwind +// context (i.e. the unwind descriptors and the user register state) between +// the unwind library and the personality routine and landing pad. Only +// selected registers may be manipulated. + +_Unwind_Word _Unwind_GetGR(_Unwind_Context*, int); +void _Unwind_SetGR(_Unwind_Context*, int, _Unwind_Word); + +_Unwind_Ptr _Unwind_GetIP(_Unwind_Context*); +_Unwind_Ptr _Unwind_GetIPInfo(_Unwind_Context*, int*); +void _Unwind_SetIP(_Unwind_Context*, _Unwind_Ptr); + +// @@@ Retrieve the CFA of the given context. +_Unwind_Word _Unwind_GetCFA(_Unwind_Context*); + +void* _Unwind_GetLanguageSpecificData(_Unwind_Context*); + +_Unwind_Ptr _Unwind_GetRegionStart(_Unwind_Context*); + + +// @@@ The following alternate entry points are for setjmp/longjmp +// based unwinding. + +struct SjLj_Function_Context; +extern void _Unwind_SjLj_Register(SjLj_Function_Context*); +extern void _Unwind_SjLj_Unregister(SjLj_Function_Context*); + +_Unwind_Reason_Code _Unwind_SjLj_RaiseException(_Unwind_Exception*); +_Unwind_Reason_Code _Unwind_SjLj_ForcedUnwind(_Unwind_Exception*, _Unwind_Stop_Fn, void*); +void _Unwind_SjLj_Resume(_Unwind_Exception*); +_Unwind_Reason_Code _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception*); + +// @@@ The following provide access to the base addresses for text +// and data-relative addressing in the LDSA. In order to stay link +// compatible with the standard ABI for IA-64, we inline these. + +version (IA64) +{ + _Unwind_Ptr _Unwind_GetDataRelBase(_Unwind_Context* _C) + { + // The GP is stored in R1. + return _Unwind_GetGR(_C, 1); + } + + _Unwind_Ptr _Unwind_GetTextRelBase(_Unwind_Context*) + { + __builtin_abort(); + return 0; + } + + // @@@ Retrieve the Backing Store Pointer of the given context. + _Unwind_Word _Unwind_GetBSP(_Unwind_Context*); +} +else +{ + _Unwind_Ptr _Unwind_GetDataRelBase(_Unwind_Context*); + _Unwind_Ptr _Unwind_GetTextRelBase(_Unwind_Context*); +} + +// @@@ Given an address, return the entry point of the function that +// contains it. +extern void* _Unwind_FindEnclosingFunction(void* pc); + + +// leb128 type numbers have a potentially unlimited size. +// The target of the following definitions of _sleb128_t and _uleb128_t +// is to have efficient data types large enough to hold the leb128 type +// numbers used in the unwind code. +// Mostly these types will simply be defined to long and unsigned long +// except when a unsigned long data type on the target machine is not +// capable of storing a pointer. + +static if (__builtin_clong.sizeof >= (void*).sizeof) +{ + alias _sleb128_t = __builtin_clong; + alias _uleb128_t = __builtin_culong; +} +else static if (long.sizeof >= (void*).sizeof) +{ + alias _sleb128_t = long; + alias _uleb128_t = ulong; +} +else +{ + static assert(false, "What type shall we use for _sleb128_t?"); +} + +version (GNU_SEH_Exceptions) +{ + // We're lazy, exact definition in MinGW/winnt.h + enum EXCEPTION_DISPOSITION + { + ExceptionContinueExecution, + ExceptionContinueSearch, + ExceptionNestedException, + ExceptionCollidedUnwind + } + + extern(C) EXCEPTION_DISPOSITION _GCC_specific_handler(void*, void*, void*, + _Unwind_Personality_Fn); +} diff --git a/libphobos/libdruntime/gcc/unwind/package.d b/libphobos/libdruntime/gcc/unwind/package.d new file mode 100644 index 0000000..0784a7a --- /dev/null +++ b/libphobos/libdruntime/gcc/unwind/package.d @@ -0,0 +1,37 @@ +// Exception handling and frame unwind runtime interface routines. +// Copyright (C) 2011-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +module gcc.unwind; + +import gcc.config; + +static if (GNU_ARM_EABI_Unwinder) +{ + version (ARM) + public import gcc.unwind.arm; + else version (TIC6X) + public import gcc.unwind.c6x; + else + static assert(false, "Unsupported target for ARM_EABI_UNWINDER"); +} +else + public import gcc.unwind.generic; diff --git a/libphobos/libdruntime/gcc/unwind/pe.d b/libphobos/libdruntime/gcc/unwind/pe.d new file mode 100644 index 0000000..caa2186 --- /dev/null +++ b/libphobos/libdruntime/gcc/unwind/pe.d @@ -0,0 +1,238 @@ +// Exception handling and frame unwind runtime interface routines. +// Copyright (C) 2011-2018 Free Software Foundation, Inc. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +// extern(C) interface for the GNU/GCC pointer encoding library. +// This corresponds to unwind-pe.h + +module gcc.unwind.pe; + +import gcc.unwind; +import gcc.builtins; + +@nogc: + +// Pointer encodings, from dwarf2.h. +enum +{ + DW_EH_PE_absptr = 0x00, + DW_EH_PE_omit = 0xff, + + DW_EH_PE_uleb128 = 0x01, + DW_EH_PE_udata2 = 0x02, + DW_EH_PE_udata4 = 0x03, + DW_EH_PE_udata8 = 0x04, + DW_EH_PE_sleb128 = 0x09, + DW_EH_PE_sdata2 = 0x0A, + DW_EH_PE_sdata4 = 0x0B, + DW_EH_PE_sdata8 = 0x0C, + DW_EH_PE_signed = 0x08, + + DW_EH_PE_pcrel = 0x10, + DW_EH_PE_textrel = 0x20, + DW_EH_PE_datarel = 0x30, + DW_EH_PE_funcrel = 0x40, + DW_EH_PE_aligned = 0x50, + + DW_EH_PE_indirect = 0x80 +} + +// Given an encoding, return the number of bytes the format occupies. +// This is only defined for fixed-size encodings, and so does not +// include leb128. +uint size_of_encoded_value(ubyte encoding) +{ + if (encoding == DW_EH_PE_omit) + return 0; + + final switch (encoding & 0x07) + { + case DW_EH_PE_absptr: + return (void*).sizeof; + case DW_EH_PE_udata2: + return 2; + case DW_EH_PE_udata4: + return 4; + case DW_EH_PE_udata8: + return 8; + } + assert(0); +} + +// Given an encoding and an _Unwind_Context, return the base to which +// the encoding is relative. This base may then be passed to +// read_encoded_value_with_base for use when the _Unwind_Context is +// not available. +_Unwind_Ptr base_of_encoded_value(ubyte encoding, _Unwind_Context* context) +{ + if (encoding == DW_EH_PE_omit) + return cast(_Unwind_Ptr) 0; + + final switch (encoding & 0x70) + { + case DW_EH_PE_absptr: + case DW_EH_PE_pcrel: + case DW_EH_PE_aligned: + return cast(_Unwind_Ptr) 0; + + case DW_EH_PE_textrel: + return _Unwind_GetTextRelBase(context); + case DW_EH_PE_datarel: + return _Unwind_GetDataRelBase(context); + case DW_EH_PE_funcrel: + return _Unwind_GetRegionStart(context); + } + assert(0); +} + +// Read an unsigned leb128 value from P, *P is incremented past the value. +// We assume that a word is large enough to hold any value so encoded; +// if it is smaller than a pointer on some target, pointers should not be +// leb128 encoded on that target. +_uleb128_t read_uleb128(const(ubyte)** p) +{ + auto q = *p; + _uleb128_t result = 0; + uint shift = 0; + + while (1) + { + ubyte b = *q++; + result |= cast(_uleb128_t)(b & 0x7F) << shift; + if ((b & 0x80) == 0) + break; + shift += 7; + } + + *p = q; + return result; +} + +// Similar, but read a signed leb128 value. +_sleb128_t read_sleb128(const(ubyte)** p) +{ + auto q = *p; + _sleb128_t result = 0; + uint shift = 0; + ubyte b = void; + + while (1) + { + b = *q++; + result |= cast(_sleb128_t)(b & 0x7F) << shift; + shift += 7; + if ((b & 0x80) == 0) + break; + } + + // Sign-extend a negative value. + if (shift < result.sizeof * 8 && (b & 0x40)) + result |= -(cast(_sleb128_t)1 << shift); + + *p = q; + return result; +} + +// Load an encoded value from memory at P. The value is returned in VAL; +// The function returns P incremented past the value. BASE is as given +// by base_of_encoded_value for this encoding in the appropriate context. +_Unwind_Ptr read_encoded_value_with_base(ubyte encoding, _Unwind_Ptr base, + const(ubyte)** p) +{ + auto q = *p; + _Unwind_Internal_Ptr result; + + if (encoding == DW_EH_PE_aligned) + { + _Unwind_Internal_Ptr a = cast(_Unwind_Internal_Ptr)q; + a = cast(_Unwind_Internal_Ptr)((a + (void*).sizeof - 1) & - (void*).sizeof); + result = *cast(_Unwind_Internal_Ptr*)a; + q = cast(ubyte*) cast(_Unwind_Internal_Ptr)(a + (void*).sizeof); + } + else + { + switch (encoding & 0x0f) + { + case DW_EH_PE_uleb128: + result = cast(_Unwind_Internal_Ptr)read_uleb128(&q); + break; + + case DW_EH_PE_sleb128: + result = cast(_Unwind_Internal_Ptr)read_sleb128(&q); + break; + + case DW_EH_PE_udata2: + result = cast(_Unwind_Internal_Ptr) *cast(ushort*)q; + q += 2; + break; + case DW_EH_PE_udata4: + result = cast(_Unwind_Internal_Ptr) *cast(uint*)q; + q += 4; + break; + case DW_EH_PE_udata8: + result = cast(_Unwind_Internal_Ptr) *cast(ulong*)q; + q += 8; + break; + + case DW_EH_PE_sdata2: + result = cast(_Unwind_Internal_Ptr) *cast(short*)q; + q += 2; + break; + case DW_EH_PE_sdata4: + result = cast(_Unwind_Internal_Ptr) *cast(int*)q; + q += 4; + break; + case DW_EH_PE_sdata8: + result = cast(_Unwind_Internal_Ptr) *cast(long*)q; + q += 8; + break; + + case DW_EH_PE_absptr: + if (size_t.sizeof == 8) + goto case DW_EH_PE_udata8; + else + goto case DW_EH_PE_udata4; + + default: + __builtin_abort(); + } + + if (result != 0) + { + result += ((encoding & 0x70) == DW_EH_PE_pcrel + ? cast(_Unwind_Internal_Ptr)*p : base); + if (encoding & DW_EH_PE_indirect) + result = *cast(_Unwind_Internal_Ptr*)result; + } + } + + *p = q; + return result; +} + +// Like read_encoded_value_with_base, but get the base from the context +// rather than providing it directly. +_Unwind_Ptr read_encoded_value(_Unwind_Context* context, ubyte encoding, + const(ubyte)** p) +{ + auto base = base_of_encoded_value(encoding, context); + return read_encoded_value_with_base(encoding, base, p); +} |