xref: /freebsd/lib/libc/arm/aeabi/aeabi_unwind_exidx.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*677a88ceSIan Lepore /*-
2*677a88ceSIan Lepore  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
3*677a88ceSIan Lepore  * All rights reserved.
4*677a88ceSIan Lepore  *
5*677a88ceSIan Lepore  * Redistribution and use in source and binary forms, with or without
6*677a88ceSIan Lepore  * modification, are permitted provided that the following conditions
7*677a88ceSIan Lepore  * are met:
8*677a88ceSIan Lepore  * 1. Redistributions of source code must retain the above copyright
9*677a88ceSIan Lepore  *    notice, this list of conditions and the following disclaimer.
10*677a88ceSIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
11*677a88ceSIan Lepore  *    notice, this list of conditions and the following disclaimer in the
12*677a88ceSIan Lepore  *    documentation and/or other materials provided with the distribution.
13*677a88ceSIan Lepore  *
14*677a88ceSIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*677a88ceSIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*677a88ceSIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*677a88ceSIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*677a88ceSIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*677a88ceSIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*677a88ceSIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*677a88ceSIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*677a88ceSIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*677a88ceSIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*677a88ceSIan Lepore  * SUCH DAMAGE.
25*677a88ceSIan Lepore  */
26*677a88ceSIan Lepore 
27*677a88ceSIan Lepore #include <sys/types.h>
28*677a88ceSIan Lepore #include <machine/elf.h>
29*677a88ceSIan Lepore #include <link.h>
30*677a88ceSIan Lepore #include <stddef.h>
31*677a88ceSIan Lepore 
32*677a88ceSIan Lepore /*
33*677a88ceSIan Lepore  * ARM EABI unwind helper.
34*677a88ceSIan Lepore  *
35*677a88ceSIan Lepore  * This finds the exidx section address and size associated with a given code
36*677a88ceSIan Lepore  * address.  There are separate implementations for static and dynamic code.
37*677a88ceSIan Lepore  *
38*677a88ceSIan Lepore  * GCC expects this function to exist as __gnu_Unwind_Find_exidx(), clang and
39*677a88ceSIan Lepore  * BSD tools expect it to be dl_unwind_find_exidx().  Both have the same API, so
40*677a88ceSIan Lepore  * we set up an alias for GCC.
41*677a88ceSIan Lepore  */
42*677a88ceSIan Lepore __strong_reference(dl_unwind_find_exidx, __gnu_Unwind_Find_exidx);
43*677a88ceSIan Lepore 
44*677a88ceSIan Lepore /*
45*677a88ceSIan Lepore  * Each entry in the exidx section is a pair of 32-bit words.  We don't
46*677a88ceSIan Lepore  * interpret the contents of the entries here; this typedef is just a local
47*677a88ceSIan Lepore  * convenience for using sizeof() and doing pointer math.
48*677a88ceSIan Lepore  */
49*677a88ceSIan Lepore typedef struct exidx_entry {
50*677a88ceSIan Lepore 	uint32_t data[2];
51*677a88ceSIan Lepore } exidx_entry;
52*677a88ceSIan Lepore 
53*677a88ceSIan Lepore #ifdef __PIC__
54*677a88ceSIan Lepore 
55*677a88ceSIan Lepore /*
56*677a88ceSIan Lepore  * Unwind helper for dynamically linked code.
57*677a88ceSIan Lepore  *
58*677a88ceSIan Lepore  * This finds the shared object that contains the given address, and returns the
59*677a88ceSIan Lepore  * address of the exidx section in that shared object along with the number of
60*677a88ceSIan Lepore  * entries in that section, or NULL if it wasn't found.
61*677a88ceSIan Lepore  */
62*677a88ceSIan Lepore void *
dl_unwind_find_exidx(const void * pc,int * pcount)63*677a88ceSIan Lepore dl_unwind_find_exidx(const void *pc, int *pcount)
64*677a88ceSIan Lepore {
65*677a88ceSIan Lepore 	const Elf_Phdr *hdr;
66*677a88ceSIan Lepore 	struct dl_phdr_info info;
67*677a88ceSIan Lepore 	int i;
68*677a88ceSIan Lepore 
69*677a88ceSIan Lepore 	if (_rtld_addr_phdr(pc, &info)) {
70*677a88ceSIan Lepore 		hdr = info.dlpi_phdr;
71*677a88ceSIan Lepore 		for (i = 0; i < info.dlpi_phnum; i++, hdr++) {
72*677a88ceSIan Lepore 			if (hdr->p_type == PT_ARM_EXIDX) {
73*677a88ceSIan Lepore 				*pcount = hdr->p_memsz / sizeof(exidx_entry);
74*677a88ceSIan Lepore 				return ((void *)(info.dlpi_addr + hdr->p_vaddr));
75*677a88ceSIan Lepore 			}
76*677a88ceSIan Lepore 		}
77*677a88ceSIan Lepore 	}
78*677a88ceSIan Lepore 	return (NULL);
79*677a88ceSIan Lepore }
80*677a88ceSIan Lepore 
81*677a88ceSIan Lepore #else	/* !__PIC__ */
82*677a88ceSIan Lepore 
83*677a88ceSIan Lepore /*
84*677a88ceSIan Lepore  * Unwind helper for statically linked code.
85*677a88ceSIan Lepore  *
86*677a88ceSIan Lepore  * In a statically linked program, the linker populates a pair of symbols with
87*677a88ceSIan Lepore  * the addresses of the start and end of the exidx table, so returning the
88*677a88ceSIan Lepore  * address and count of elements is pretty straighforward.
89*677a88ceSIan Lepore  */
90*677a88ceSIan Lepore void *
dl_unwind_find_exidx(const void * pc,int * pcount)91*677a88ceSIan Lepore dl_unwind_find_exidx(const void *pc, int *pcount)
92*677a88ceSIan Lepore {
93*677a88ceSIan Lepore 	extern struct exidx_entry __exidx_start;
94*677a88ceSIan Lepore 	extern struct exidx_entry __exidx_end;
95*677a88ceSIan Lepore 
96*677a88ceSIan Lepore 	*pcount = (int)(&__exidx_end - &__exidx_start);
97*677a88ceSIan Lepore 	return (&__exidx_start);
98*677a88ceSIan Lepore }
99*677a88ceSIan Lepore 
100*677a88ceSIan Lepore #endif	/* __PIC__ */
101*677a88ceSIan Lepore 
102