1 /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library.  If not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <link.h>
19 #include <unwind.h>
20 
21 struct unw_eh_callback_data
22 {
23   _Unwind_Ptr pc;
24   _Unwind_Ptr exidx_start;
25   int exidx_len;
26 };
27 
28 
29 /* Callback to determins if the PC lies within an object, and remember the
30    location of the exception index table if it does.  */
31 
32 static int
find_exidx_callback(struct dl_phdr_info * info,size_t size,void * ptr)33 find_exidx_callback (struct dl_phdr_info * info, size_t size, void * ptr)
34 {
35   struct unw_eh_callback_data * data;
36   const ElfW(Phdr) *phdr;
37   int i;
38   int match;
39   _Unwind_Ptr load_base;
40 
41   data = (struct unw_eh_callback_data *) ptr;
42   load_base = info->dlpi_addr;
43   phdr = info->dlpi_phdr;
44 
45   match = 0;
46   for (i = info->dlpi_phnum; i > 0; i--, phdr++)
47     {
48       if (phdr->p_type == PT_LOAD)
49         {
50           _Unwind_Ptr vaddr = phdr->p_vaddr + load_base;
51           if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
52             match = 1;
53         }
54       else if (phdr->p_type == PT_ARM_EXIDX)
55 	{
56 	  data->exidx_start = (_Unwind_Ptr) (phdr->p_vaddr + load_base);
57 	  data->exidx_len = phdr->p_memsz;
58 	}
59     }
60 
61   return match;
62 }
63 
64 
65 /* Find the exception index table containing PC.  */
66 
67 _Unwind_Ptr
__gnu_Unwind_Find_exidx(_Unwind_Ptr pc,int * pcount)68 __gnu_Unwind_Find_exidx (_Unwind_Ptr pc, int * pcount)
69 {
70   struct unw_eh_callback_data data;
71 
72   data.pc = pc;
73   data.exidx_start = 0;
74   if (__dl_iterate_phdr (find_exidx_callback, &data) <= 0)
75     return 0;
76 
77   *pcount = data.exidx_len / 8;
78   return data.exidx_start;
79 }
80