2 * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <machine/elf.h>
36 * ARM EABI unwind helper.
38 * This finds the exidx section address and size associated with a given code
39 * address. There are separate implementations for static and dynamic code.
41 * GCC expects this function to exist as __gnu_Unwind_Find_exidx(), clang and
42 * BSD tools expect it to be dl_unwind_find_exidx(). Both have the same API, so
43 * we set up an alias for GCC.
45 __strong_reference(dl_unwind_find_exidx, __gnu_Unwind_Find_exidx);
48 * Each entry in the exidx section is a pair of 32-bit words. We don't
49 * interpret the contents of the entries here; this typedef is just a local
50 * convenience for using sizeof() and doing pointer math.
52 typedef struct exidx_entry {
59 * Unwind helper for dynamically linked code.
61 * This finds the shared object that contains the given address, and returns the
62 * address of the exidx section in that shared object along with the number of
63 * entries in that section, or NULL if it wasn't found.
66 dl_unwind_find_exidx(const void *pc, int *pcount)
69 struct dl_phdr_info info;
72 if (_rtld_addr_phdr(pc, &info)) {
74 for (i = 0; i < info.dlpi_phnum; i++, hdr++) {
75 if (hdr->p_type == PT_ARM_EXIDX) {
76 *pcount = hdr->p_memsz / sizeof(exidx_entry);
77 return ((void *)(info.dlpi_addr + hdr->p_vaddr));
87 * Unwind helper for statically linked code.
89 * In a statically linked program, the linker populates a pair of symbols with
90 * the addresses of the start and end of the exidx table, so returning the
91 * address and count of elements is pretty straighforward.
94 dl_unwind_find_exidx(const void *pc, int *pcount)
96 extern struct exidx_entry __exidx_start;
97 extern struct exidx_entry __exidx_end;
99 *pcount = (int)(&__exidx_end - &__exidx_start);
100 return (&__exidx_start);