11573Srgrimes/*-
21573Srgrimes * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
31573Srgrimes * All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes *
141573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16249808Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241573Srgrimes * SUCH DAMAGE.
251573Srgrimes */
261573Srgrimes
271573Srgrimes#include <sys/cdefs.h>
281573Srgrimes__FBSDID("$FreeBSD: releng/10.3/lib/libc/arm/aeabi/aeabi_unwind_exidx.c 269792 2014-08-10 22:26:29Z ian $");
291573Srgrimes
301573Srgrimes#include <sys/types.h>
311573Srgrimes#include <machine/elf.h>
321573Srgrimes#include <link.h>
331573Srgrimes#include <stddef.h>
341573Srgrimes
351573Srgrimes/*
3692986Sobrien * ARM EABI unwind helper.
3792986Sobrien *
381573Srgrimes * This finds the exidx section address and size associated with a given code
3971579Sdeischen * address.  There are separate implementations for static and dynamic code.
401573Srgrimes *
4171579Sdeischen * GCC expects this function to exist as __gnu_Unwind_Find_exidx(), clang and
42101776Stjr * BSD tools expect it to be dl_unwind_find_exidx().  Both have the same API, so
4335129Sjb * we set up an alias for GCC.
441573Srgrimes */
4513545Sjulian__strong_reference(dl_unwind_find_exidx, __gnu_Unwind_Find_exidx);
46249810Semaste
471573Srgrimes/*
4813545Sjulian * Each entry in the exidx section is a pair of 32-bit words.  We don't
4935129Sjb * interpret the contents of the entries here; this typedef is just a local
50127198Stjr * convenience for using sizeof() and doing pointer math.
51127198Stjr */
52126806Stjrtypedef struct exidx_entry {
5335129Sjb	uint32_t data[2];
5413545Sjulian} exidx_entry;
551573Srgrimes
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 */
65void *
66dl_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 */
93void *
94dl_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