For some reason ARM wants to be different.  The EABI exception
handling ABI (EHABI for short) uses an ARM.exidx section and the
PT_ARM_EXIDX entry to find the exeption handling information, and libc
(or ld.so) needs to provide a helper function to support this.  In the
GCC world, the function is named __gnu_Unwind_Find_exidx.  In the
llvm/libunwind world, FreeBSD and NetBSD call the function
dl_unwind_find_exidx.  The diff below adds the code and provides the
function under both names (which is what FreeBSD and NetBSD do).

Note that libunwind uses __gnu_Unwind_Find_exidx on Linux, so we could
follow Linux here and drop the dl_unwind_find_exidx alias.  But my
personal preference is to follow the other BSDs here.

With this diff, exception handling works again for C++ code compiled
with g++.  Still chasing down an issue with clang++.  But I'm fairly
certain this code is correct for both use cases.

ok?


Index: arch/arm/Symbols.list
===================================================================
RCS file: /cvs/src/lib/libc/arch/arm/Symbols.list,v
retrieving revision 1.3
diff -u -p -r1.3 Symbols.list
--- arch/arm/Symbols.list       30 May 2016 05:18:52 -0000      1.3
+++ arch/arm/Symbols.list       17 Sep 2016 16:04:32 -0000
@@ -85,3 +85,7 @@ shortShift64Left
 shortShift96Left
 sub64
 sub96
+
+/* dlfcn */
+dl_unwind_find_exidx
+__gnu_Unwind_Find_exidx
Index: dlfcn/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/dlfcn/Makefile.inc,v
retrieving revision 1.5
diff -u -p -r1.5 Makefile.inc
--- dlfcn/Makefile.inc  7 May 2016 19:05:22 -0000       1.5
+++ dlfcn/Makefile.inc  17 Sep 2016 16:04:32 -0000
@@ -5,3 +5,7 @@
 .include <bsd.own.mk>
 
 SRCS+=         dlfcn_stubs.c init.c tib.c
+
+.if ${MACHINE_ARCH} == "arm"
+SRCS+=         exidx.c
+.endif
Index: dlfcn/exidx.c
===================================================================
RCS file: dlfcn/exidx.c
diff -N dlfcn/exidx.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ dlfcn/exidx.c       17 Sep 2016 16:04:32 -0000
@@ -0,0 +1,75 @@
+/*     $OpenBSD$ */
+/*
+ * Copyright (c) 2016 Mark Kettenis <kette...@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <link.h>
+
+void *dl_unwind_find_exidx(const void *pc, int *pcount) __attribute__((weak));
+
+struct exidx_data {
+       u_long pc;
+       void *exidx;
+       int *pcount;
+};
+
+struct exidx_entry {
+       uint32_t data[2];
+};
+
+static int
+find_exidx(struct dl_phdr_info *info, size_t size, void *p)
+{
+       struct exidx_data *data = p;
+       const Elf_Phdr *phdr;
+       void *exidx;
+       int count = 0;
+       int found = 0;
+       int i;
+
+       for (i = 0; i < info->dlpi_phnum; i++) {
+               phdr = &info->dlpi_phdr[i];
+               if (data->pc >= info->dlpi_addr + phdr->p_vaddr &&
+                   data->pc < info->dlpi_addr + phdr->p_vaddr + phdr->p_memsz)
+                       found = 1;
+               if (phdr->p_type == PT_ARM_EXIDX) {
+                       exidx = (void *)(info->dlpi_addr + phdr->p_vaddr);
+                       count = phdr->p_memsz / sizeof(struct exidx_entry);
+               }
+       }
+
+       if (found && count > 0) {
+               data->exidx = exidx;
+               *data->pcount = count;
+               return 1;
+       }
+
+       return 0;
+}
+
+void *
+dl_unwind_find_exidx(const void *pc, int *pcount)
+{
+       struct exidx_data data;
+
+       data.pc = (u_long)pc;
+       data.pcount = pcount;
+       if (dl_iterate_phdr(find_exidx, &data))
+               return data.exidx;
+       return NULL;
+}
+
+__strong_alias(__gnu_Unwind_Find_exidx, dl_unwind_find_exidx);

Reply via email to