xref: /freebsd/lib/libc/arm/aeabi/aeabi_unwind_exidx.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <machine/elf.h>
30 #include <link.h>
31 #include <stddef.h>
32 
33 /*
34  * ARM EABI unwind helper.
35  *
36  * This finds the exidx section address and size associated with a given code
37  * address.  There are separate implementations for static and dynamic code.
38  *
39  * GCC expects this function to exist as __gnu_Unwind_Find_exidx(), clang and
40  * BSD tools expect it to be dl_unwind_find_exidx().  Both have the same API, so
41  * we set up an alias for GCC.
42  */
43 __strong_reference(dl_unwind_find_exidx, __gnu_Unwind_Find_exidx);
44 
45 /*
46  * Each entry in the exidx section is a pair of 32-bit words.  We don't
47  * interpret the contents of the entries here; this typedef is just a local
48  * convenience for using sizeof() and doing pointer math.
49  */
50 typedef struct exidx_entry {
51 	uint32_t data[2];
52 } exidx_entry;
53 
54 #ifdef __PIC__
55 
56 /*
57  * Unwind helper for dynamically linked code.
58  *
59  * This finds the shared object that contains the given address, and returns the
60  * address of the exidx section in that shared object along with the number of
61  * entries in that section, or NULL if it wasn't found.
62  */
63 void *
64 dl_unwind_find_exidx(const void *pc, int *pcount)
65 {
66 	const Elf_Phdr *hdr;
67 	struct dl_phdr_info info;
68 	int i;
69 
70 	if (_rtld_addr_phdr(pc, &info)) {
71 		hdr = info.dlpi_phdr;
72 		for (i = 0; i < info.dlpi_phnum; i++, hdr++) {
73 			if (hdr->p_type == PT_ARM_EXIDX) {
74 				*pcount = hdr->p_memsz / sizeof(exidx_entry);
75 				return ((void *)(info.dlpi_addr + hdr->p_vaddr));
76 			}
77 		}
78 	}
79 	return (NULL);
80 }
81 
82 #else	/* !__PIC__ */
83 
84 /*
85  * Unwind helper for statically linked code.
86  *
87  * In a statically linked program, the linker populates a pair of symbols with
88  * the addresses of the start and end of the exidx table, so returning the
89  * address and count of elements is pretty straighforward.
90  */
91 void *
92 dl_unwind_find_exidx(const void *pc, int *pcount)
93 {
94 	extern struct exidx_entry __exidx_start;
95 	extern struct exidx_entry __exidx_end;
96 
97 	*pcount = (int)(&__exidx_end - &__exidx_start);
98 	return (&__exidx_start);
99 }
100 
101 #endif	/* __PIC__ */
102 
103