184d9c625SLionel Sambuc /*-
284d9c625SLionel Sambuc * Copyright (c) 2013 The NetBSD Foundation, Inc.
384d9c625SLionel Sambuc * All rights reserved.
484d9c625SLionel Sambuc *
584d9c625SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
684d9c625SLionel Sambuc * by Matt Thomas of 3am Software Foundry.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
984d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1084d9c625SLionel Sambuc * are met:
1184d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1284d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1384d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1484d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1584d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
1684d9c625SLionel Sambuc *
1784d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1884d9c625SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1984d9c625SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2084d9c625SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2184d9c625SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2284d9c625SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2384d9c625SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2484d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2584d9c625SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2684d9c625SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2784d9c625SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2884d9c625SLionel Sambuc */
2984d9c625SLionel Sambuc
3084d9c625SLionel Sambuc #include <sys/cdefs.h>
3184d9c625SLionel Sambuc #ifndef lint
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: find_exidx.c,v 1.4 2014/08/10 23:35:27 matt Exp $");
3384d9c625SLionel Sambuc #endif /* not lint */
3484d9c625SLionel Sambuc
3584d9c625SLionel Sambuc #include "debug.h"
3684d9c625SLionel Sambuc #include "rtld.h"
3784d9c625SLionel Sambuc
38*0a6a1f1dSLionel Sambuc #if defined(__ARM_EABI__) && !defined(__ARM_DWARF_EH__)
39*0a6a1f1dSLionel Sambuc
4084d9c625SLionel Sambuc _Unwind_Ptr
__gnu_Unwind_Find_exidx(_Unwind_Ptr pc,int * pcount)4184d9c625SLionel Sambuc __gnu_Unwind_Find_exidx(_Unwind_Ptr pc, int * pcount)
4284d9c625SLionel Sambuc {
4384d9c625SLionel Sambuc const Obj_Entry *obj;
4484d9c625SLionel Sambuc _Unwind_Ptr start = NULL;
4584d9c625SLionel Sambuc int count = 0;
4684d9c625SLionel Sambuc
4784d9c625SLionel Sambuc dbg(("__gnu_Unwind_Find_exidx"));
4884d9c625SLionel Sambuc
4984d9c625SLionel Sambuc _rtld_shared_enter();
5084d9c625SLionel Sambuc
5184d9c625SLionel Sambuc vaddr_t va = (vaddr_t)pc;
5284d9c625SLionel Sambuc for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
5384d9c625SLionel Sambuc /*
5484d9c625SLionel Sambuc * If the address we are looking for is inside this object,
5584d9c625SLionel Sambuc * we've found the object to inspect.
5684d9c625SLionel Sambuc */
5784d9c625SLionel Sambuc if ((vaddr_t)obj->mapbase <= va
5884d9c625SLionel Sambuc && va < (vaddr_t)obj->mapbase + obj->mapsize)
5984d9c625SLionel Sambuc break;
6084d9c625SLionel Sambuc }
6184d9c625SLionel Sambuc
6284d9c625SLionel Sambuc /*
6384d9c625SLionel Sambuc * If we found an object and it has some exception data, we
6484d9c625SLionel Sambuc * need to see if the address matches a PT_LOAD section.
6584d9c625SLionel Sambuc */
6684d9c625SLionel Sambuc if (obj != NULL && obj->exidx_start != NULL) {
6784d9c625SLionel Sambuc va -= (vaddr_t)obj->relocbase;
6884d9c625SLionel Sambuc const Elf_Phdr *ph = obj->phdr;
6984d9c625SLionel Sambuc const Elf_Phdr * const phlimit = ph + obj->phsize / sizeof(*ph);
7084d9c625SLionel Sambuc for (; ph < phlimit; ph++) {
7184d9c625SLionel Sambuc if (ph->p_type == PT_LOAD
7284d9c625SLionel Sambuc && ph->p_vaddr <= va
7384d9c625SLionel Sambuc && va < ph->p_vaddr + ph->p_memsz) {
7484d9c625SLionel Sambuc count = obj->exidx_sz / 8;
7584d9c625SLionel Sambuc start = obj->exidx_start;
7684d9c625SLionel Sambuc break;
7784d9c625SLionel Sambuc }
7884d9c625SLionel Sambuc }
7984d9c625SLionel Sambuc }
8084d9c625SLionel Sambuc
8184d9c625SLionel Sambuc _rtld_shared_exit();
8284d9c625SLionel Sambuc
8384d9c625SLionel Sambuc /*
8484d9c625SLionel Sambuc * deal with the return values.
8584d9c625SLionel Sambuc */
8684d9c625SLionel Sambuc *pcount = count;
8784d9c625SLionel Sambuc return start;
8884d9c625SLionel Sambuc }
89*0a6a1f1dSLionel Sambuc
90*0a6a1f1dSLionel Sambuc #endif
91