xref: /freebsd/lib/libc/arm/aeabi/aeabi_unwind_exidx.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <machine/elf.h>
32 #include <link.h>
33 #include <stddef.h>
34 
35 /*
36  * ARM EABI unwind helper.
37  *
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.
40  *
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.
44  */
45 __strong_reference(dl_unwind_find_exidx, __gnu_Unwind_Find_exidx);
46 
47 /*
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.
51  */
52 typedef struct exidx_entry {
53 	uint32_t data[2];
54 } exidx_entry;
55 
56 #ifdef __PIC__
57 
58 /*
59  * Unwind helper for dynamically linked code.
60  *
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.
64  */
65 void *
66 dl_unwind_find_exidx(const void *pc, int *pcount)
67 {
68 	const Elf_Phdr *hdr;
69 	struct dl_phdr_info info;
70 	int i;
71 
72 	if (_rtld_addr_phdr(pc, &info)) {
73 		hdr = info.dlpi_phdr;
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));
78 			}
79 		}
80 	}
81 	return (NULL);
82 }
83 
84 #else	/* !__PIC__ */
85 
86 /*
87  * Unwind helper for statically linked code.
88  *
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.
92  */
93 void *
94 dl_unwind_find_exidx(const void *pc, int *pcount)
95 {
96 	extern struct exidx_entry __exidx_start;
97 	extern struct exidx_entry __exidx_end;
98 
99 	*pcount = (int)(&__exidx_end - &__exidx_start);
100 	return (&__exidx_start);
101 }
102 
103 #endif	/* __PIC__ */
104 
105