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