From 499fe48de8938d4c7b0a91e20eb6c16db9d55633 Mon Sep 17 00:00:00 2001 From: Conrad Meyer Date: Sat, 26 Oct 2019 06:59:59 +0000 Subject: Sync up with NetBSD libexecinfo changes 2014-2019 Drop portions that are unlit or redundant with llvm-libunwind: builtin.c, unwind.h, and unwind_arm_ehabi_stub.c. This code should now work with -fPIE binaries, should we choose to build any that way. When backtrace() array is full, signal an error so the underlying Itanium-style C++ exception handling library (llvm-libunwind) knows to stop tracing instead of continuing. (It should stop on its own when it finishes unwinding, so this is mostly an extra seatbelt against an infinite loop bug in the unwinder.) --- contrib/libexecinfo/builtin.c | 68 ----------------------------- contrib/libexecinfo/symtab.c | 46 ++++++++++++++++--- contrib/libexecinfo/unwind.c | 8 ++-- contrib/libexecinfo/unwind.h | 68 ----------------------------- contrib/libexecinfo/unwind_arm_ehabi_stub.c | 61 -------------------------- lib/libexecinfo/Makefile | 1 + 6 files changed, 45 insertions(+), 207 deletions(-) delete mode 100644 contrib/libexecinfo/builtin.c delete mode 100644 contrib/libexecinfo/unwind.h delete mode 100644 contrib/libexecinfo/unwind_arm_ehabi_stub.c diff --git a/contrib/libexecinfo/builtin.c b/contrib/libexecinfo/builtin.c deleted file mode 100644 index 3fc861958d15..000000000000 --- a/contrib/libexecinfo/builtin.c +++ /dev/null @@ -1,68 +0,0 @@ -/* $NetBSD: builtin.c,v 1.1 2012/05/26 22:02:29 christos Exp $ */ - -/*- - * Copyright (c) 2012 The NetBSD Foundation, Inc. - * All rights reserved. - * - * This code is derived from software contributed to The NetBSD Foundation - * by Christos Zoulas. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include -__RCSID("$NetBSD: builtin.c,v 1.1 2012/05/26 22:02:29 christos Exp $"); - -#include -#include - -#include "execinfo.h" - -#ifdef __MACHINE_STACK_GROWS_UP -#define BELOW > -#else -#define BELOW < -#endif - -#ifdef __lint__ -#define __builtin_frame_address(a) ((void *)a) -#endif - -struct frameinfo { - struct frameinfo *next; - void *return_address; -}; - -size_t -backtrace(void **trace, size_t len) -{ - const struct frameinfo *frame = __builtin_frame_address(0); - void *stack = &stack; - - for (size_t i = 0; i < len; i++) { - if ((const void *)frame BELOW stack) - return i; - trace[i] = frame->return_address; - frame = frame->next; - } - - return len; -} diff --git a/contrib/libexecinfo/symtab.c b/contrib/libexecinfo/symtab.c index 92d92dc20bc5..dda1ba2dd78e 100644 --- a/contrib/libexecinfo/symtab.c +++ b/contrib/libexecinfo/symtab.c @@ -1,4 +1,4 @@ -/* $NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $ */ +/* $NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -29,12 +29,13 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include -__RCSID("$NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $"); +__RCSID("$NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $"); #include #include #include #include +#include #include #include @@ -47,7 +48,6 @@ __RCSID("$NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $"); #define ELF_ST_TYPE(x) (((unsigned int)x) & 0xf) #endif - #include "symtab.h" struct symbol { @@ -59,6 +59,7 @@ struct symbol { struct symtab { size_t nsymbols; struct symbol *symbols; + bool ispie; }; static int @@ -86,6 +87,7 @@ symtab_create(int fd, int bind, int type) Elf *elf; symtab_t *st; Elf_Scn *scn = NULL; + GElf_Ehdr ehdr; if (elf_version(EV_CURRENT) == EV_NONE) { warnx("Elf Library is out of date."); @@ -103,6 +105,13 @@ symtab_create(int fd, int bind, int type) elf_end(elf); return NULL; } + if (gelf_getehdr(elf, &ehdr) == NULL) { + warnx("Error getting ELF Ehdr"); + elf_end(elf); + return NULL; + } + + st->ispie = ehdr.e_type == ET_DYN; while ((scn = elf_nextscn(elf, scn)) != NULL) { GElf_Shdr shdr; @@ -127,6 +136,13 @@ symtab_create(int fd, int bind, int type) GElf_Sym sym; gelf_getsym(edata, (int)i, &sym); +#ifdef SYMTAB_DEBUG + fprintf(stderr, "%s: %s@%#jx=%d,%d\n", __func__, + elf_strptr(elf, shdr.sh_link, sym.st_name), + (uintmax_t)sym.st_value, ELF_ST_BIND(sym.st_info), + ELF_ST_TYPE(sym.st_info)); +#endif + if (bind != -1 && (unsigned)bind != ELF_ST_BIND(sym.st_info)) continue; @@ -159,7 +175,7 @@ out: return NULL; } - + int symtab_find(const symtab_t *st, const void *p, Dl_info *dli) { @@ -168,8 +184,15 @@ symtab_find(const symtab_t *st, const void *p, Dl_info *dli) size_t hi = ns; size_t lo = 0; size_t mid = ns / 2; - uintptr_t dd, sd, me = (uintptr_t)p; - + uintptr_t fbase = st->ispie ? (uintptr_t)dli->dli_fbase : 0; + uintptr_t dd, sd, me = (uintptr_t)p - fbase; + uintptr_t ad = (uintptr_t)dli->dli_saddr - fbase; + +#ifdef SYMTAB_DEBUG + fprintf(stderr, "%s: [fbase=%#jx, saddr=%p, me=%#jx ad=%#jx]\n", + __func__, (uintmax_t)fbase, dli->dli_saddr, (uintmax_t)me, + (uintmax_t)ad); +#endif for (;;) { if (s[mid].st_value < me) lo = mid; @@ -183,11 +206,20 @@ symtab_find(const symtab_t *st, const void *p, Dl_info *dli) } mid = (hi + lo) / 2; } - dd = me - (uintptr_t)dli->dli_saddr; + dd = me - ad; sd = me - s[mid].st_value; if (dd > sd) { dli->dli_saddr = (void *)s[mid].st_value; dli->dli_sname = s[mid].st_name; +#ifdef SYMTAB_DEBUG + fprintf(stderr, "%s: me=%#jx -> [%#jx, %s]\n", __func__, + (uintmax_t)me, (uintmax_t)sd, dli->dli_sname); +#endif } +#ifdef SYMTAB_DEBUG + else + fprintf(stderr, "%s: %#jx -> [%#jx, ***]\n", __func__, + (uintmax_t)me, (uintmax_t)sd); +#endif return 1; } diff --git a/contrib/libexecinfo/unwind.c b/contrib/libexecinfo/unwind.c index b9b785ff6021..3b489168ebaa 100644 --- a/contrib/libexecinfo/unwind.c +++ b/contrib/libexecinfo/unwind.c @@ -1,4 +1,4 @@ -/* $NetBSD: unwind.c,v 1.1 2012/05/26 22:02:29 christos Exp $ */ +/* $NetBSD: unwind.c,v 1.3 2019/01/30 22:46:49 mrg Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -51,8 +51,10 @@ tracer(struct _Unwind_Context *ctx, void *arg) return 0; } if (t->n < t->len) - t->arr[t->n++] = _Unwind_GetIP(ctx); - return 0; + t->arr[t->n++] = (void *)_Unwind_GetIP(ctx); + else + return _URC_END_OF_STACK; + return _URC_NO_REASON; } size_t diff --git a/contrib/libexecinfo/unwind.h b/contrib/libexecinfo/unwind.h deleted file mode 100644 index c9978b3d5df5..000000000000 --- a/contrib/libexecinfo/unwind.h +++ /dev/null @@ -1,68 +0,0 @@ -/* $NetBSD: unwind.h,v 1.1 2012/05/26 22:02:29 christos Exp $ */ - -/*- - * Copyright (c) 2012 The NetBSD Foundation, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef _UNWIND_H_ -#define _UNWIND_H_ - -__BEGIN_DECLS -struct _Unwind_Context; -struct _Unwind_Exception; -typedef int _Unwind_Reason_Code; -typedef void *_Unwind_Ptr; -typedef long _Unwind_Word; - -typedef _Unwind_Reason_Code - (*_Unwind_Trace_Fn)(struct _Unwind_Context *, void *); -#ifdef notyet -typedef _Unwind_Reason_Code - (*_Unwind_Stop_Fn)(struct _Unwind_Context *, void *); -#endif - -_Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *); -void _Unwind_DeleteException(struct _Unwind_Exception *); -void *_Unwind_FindEnclosingFunction(void *); -#ifdef notyet -_Unwind_Reason_Code _Unwind_ForcedUnwind(struct _Unwind_Exception *, - _Unwind_Stop_fn, void *); -#endif -_Unwind_Word _Unwind_GetCFA(struct _Unwind_Context *); -_Unwind_Ptr _Unwind_GetDataRelBase(struct _Unwind_Context *); -_Unwind_Word _Unwind_GetGR(struct _Unwind_Context *, int); -_Unwind_Ptr _Unwind_GetIP(struct _Unwind_Context *); -_Unwind_Ptr _Unwind_GetIPInfo(struct _Unwind_Context *, int *); -void *_Unwind_GetLanguageSpecificData( - struct _Unwind_Context *); -_Unwind_Ptr _Unwind_GetRegionStart(struct _Unwind_Context *); -_Unwind_Ptr _Unwind_GetTextRelBase(struct _Unwind_Context *); -_Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *); -void _Unwind_Resume(struct _Unwind_Exception *); -_Unwind_Reason_Code _Unwind_Resume_or_Rethrow(struct _Unwind_Exception *); -void _Unwind_SetGR(struct _Unwind_Context *, int, - _Unwind_Ptr); -void _Unwind_SetIP(struct _Unwind_Context *, _Unwind_Ptr); -__END_DECLS -#endif /* _UNWIND_H_ */ diff --git a/contrib/libexecinfo/unwind_arm_ehabi_stub.c b/contrib/libexecinfo/unwind_arm_ehabi_stub.c deleted file mode 100644 index 6fe97161edf5..000000000000 --- a/contrib/libexecinfo/unwind_arm_ehabi_stub.c +++ /dev/null @@ -1,61 +0,0 @@ -/*- - * Copyright (c) 2013 The NetBSD Foundation, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include -#include -#include "unwind.h" - -void _Unwind_VRS_Get(struct _Unwind_Context *, int, _Unwind_Word, int, void *); -void _Unwind_VRS_Set(struct _Unwind_Context *, int, _Unwind_Word, int, void *); - -_Unwind_Word -_Unwind_GetGR(struct _Unwind_Context *context, int regno) -{ - _Unwind_Word val; - _Unwind_VRS_Get(context, 0 /*_UVRSC_CORE*/, regno, 0 /*_UVRSD_UINT32*/, - &val); - - return val; -} - -_Unwind_Ptr -_Unwind_GetIP(struct _Unwind_Context *context) -{ - return (_Unwind_Ptr)(_Unwind_GetGR (context, 15) & ~(_Unwind_Word)1); -} - -_Unwind_Ptr -_Unwind_GetIPInfo(struct _Unwind_Context *context, int *p) -{ - *p = 0; - return _Unwind_GetIP(context); -} - -void -_Unwind_SetGR(struct _Unwind_Context *context, int reg, _Unwind_Ptr val) -{ - _Unwind_VRS_Set(context, 0 /*_UVRSC_CORE*/, reg, 0 /*_UVRSD_UINT32*/, - &val); -} diff --git a/lib/libexecinfo/Makefile b/lib/libexecinfo/Makefile index 9086d18d4347..fed41fa1d4a3 100644 --- a/lib/libexecinfo/Makefile +++ b/lib/libexecinfo/Makefile @@ -10,6 +10,7 @@ SHLIB_MAJOR= 1 INCS= execinfo.h SRCS= backtrace.c symtab.c unwind.c +CFLAGS+= -I${SRCTOP}/contrib/libunwind/include LIBADD= elf -- cgit v1.2.3