1*c87b03e5Sespie /* Copyright (C) 2001, 2002 Free Software Foundation, Inc.
2*c87b03e5Sespie    Contributed by Jakub Jelinek <jakub@redhat.com>.
3*c87b03e5Sespie 
4*c87b03e5Sespie    This file is part of GNU CC.
5*c87b03e5Sespie 
6*c87b03e5Sespie    GNU CC is free software; you can redistribute it and/or modify
7*c87b03e5Sespie    it under the terms of the GNU General Public License as published by
8*c87b03e5Sespie    the Free Software Foundation; either version 2, or (at your option)
9*c87b03e5Sespie    any later version.
10*c87b03e5Sespie 
11*c87b03e5Sespie    GNU CC is distributed in the hope that it will be useful,
12*c87b03e5Sespie    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c87b03e5Sespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*c87b03e5Sespie    GNU General Public License for more details.
15*c87b03e5Sespie 
16*c87b03e5Sespie    You should have received a copy of the GNU General Public License
17*c87b03e5Sespie    along with GNU CC; see the file COPYING.  If not, write to
18*c87b03e5Sespie    the Free Software Foundation, 59 Temple Place - Suite 330,
19*c87b03e5Sespie    Boston, MA 02111-1307, USA.  */
20*c87b03e5Sespie 
21*c87b03e5Sespie /* As a special exception, if you link this library with other files,
22*c87b03e5Sespie    some of which are compiled with GCC, to produce an executable,
23*c87b03e5Sespie    this library does not by itself cause the resulting executable
24*c87b03e5Sespie    to be covered by the GNU General Public License.
25*c87b03e5Sespie    This exception does not however invalidate any other reasons why
26*c87b03e5Sespie    the executable file might be covered by the GNU General Public License.  */
27*c87b03e5Sespie 
28*c87b03e5Sespie /* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF
29*c87b03e5Sespie    segment and dl_iterate_phdr to avoid register/deregister calls at
30*c87b03e5Sespie    DSO load/unload.  */
31*c87b03e5Sespie 
32*c87b03e5Sespie #ifndef _GNU_SOURCE
33*c87b03e5Sespie #define _GNU_SOURCE 1
34*c87b03e5Sespie #endif
35*c87b03e5Sespie 
36*c87b03e5Sespie #include "auto-host.h" /* For HAVE_LD_EH_FRAME_HDR.  */
37*c87b03e5Sespie #include "tconfig.h"
38*c87b03e5Sespie #ifndef inhibit_libc
39*c87b03e5Sespie #include <stddef.h>
40*c87b03e5Sespie #include <stdlib.h>
41*c87b03e5Sespie #include <link.h>
42*c87b03e5Sespie #endif
43*c87b03e5Sespie #include "tsystem.h"
44*c87b03e5Sespie #include "dwarf2.h"
45*c87b03e5Sespie #include "unwind.h"
46*c87b03e5Sespie #define NO_BASE_OF_ENCODED_VALUE
47*c87b03e5Sespie #include "unwind-pe.h"
48*c87b03e5Sespie #include "unwind-dw2-fde.h"
49*c87b03e5Sespie #include "gthr.h"
50*c87b03e5Sespie 
51*c87b03e5Sespie #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
52*c87b03e5Sespie     && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
53*c87b03e5Sespie 	|| (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
54*c87b03e5Sespie 
55*c87b03e5Sespie static fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases);
56*c87b03e5Sespie 
57*c87b03e5Sespie #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
58*c87b03e5Sespie #include "unwind-dw2-fde.c"
59*c87b03e5Sespie #undef _Unwind_Find_FDE
60*c87b03e5Sespie 
61*c87b03e5Sespie #ifndef PT_GNU_EH_FRAME
62*c87b03e5Sespie #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
63*c87b03e5Sespie #endif
64*c87b03e5Sespie 
65*c87b03e5Sespie struct unw_eh_callback_data
66*c87b03e5Sespie {
67*c87b03e5Sespie   _Unwind_Ptr pc;
68*c87b03e5Sespie   void *tbase;
69*c87b03e5Sespie   void *dbase;
70*c87b03e5Sespie   void *func;
71*c87b03e5Sespie   fde *ret;
72*c87b03e5Sespie };
73*c87b03e5Sespie 
74*c87b03e5Sespie struct unw_eh_frame_hdr
75*c87b03e5Sespie {
76*c87b03e5Sespie   unsigned char version;
77*c87b03e5Sespie   unsigned char eh_frame_ptr_enc;
78*c87b03e5Sespie   unsigned char fde_count_enc;
79*c87b03e5Sespie   unsigned char table_enc;
80*c87b03e5Sespie };
81*c87b03e5Sespie 
82*c87b03e5Sespie /* Like base_of_encoded_value, but take the base from a struct
83*c87b03e5Sespie    unw_eh_callback_data instead of an _Unwind_Context.  */
84*c87b03e5Sespie 
85*c87b03e5Sespie static _Unwind_Ptr
base_from_cb_data(unsigned char encoding,struct unw_eh_callback_data * data)86*c87b03e5Sespie base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
87*c87b03e5Sespie {
88*c87b03e5Sespie   if (encoding == DW_EH_PE_omit)
89*c87b03e5Sespie     return 0;
90*c87b03e5Sespie 
91*c87b03e5Sespie   switch (encoding & 0x70)
92*c87b03e5Sespie     {
93*c87b03e5Sespie     case DW_EH_PE_absptr:
94*c87b03e5Sespie     case DW_EH_PE_pcrel:
95*c87b03e5Sespie     case DW_EH_PE_aligned:
96*c87b03e5Sespie       return 0;
97*c87b03e5Sespie 
98*c87b03e5Sespie     case DW_EH_PE_textrel:
99*c87b03e5Sespie       return (_Unwind_Ptr) data->tbase;
100*c87b03e5Sespie     case DW_EH_PE_datarel:
101*c87b03e5Sespie       return (_Unwind_Ptr) data->dbase;
102*c87b03e5Sespie     }
103*c87b03e5Sespie   abort ();
104*c87b03e5Sespie }
105*c87b03e5Sespie 
106*c87b03e5Sespie static int
_Unwind_IteratePhdrCallback(struct dl_phdr_info * info,size_t size,void * ptr)107*c87b03e5Sespie _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
108*c87b03e5Sespie {
109*c87b03e5Sespie   struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
110*c87b03e5Sespie   const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
111*c87b03e5Sespie   long n, match;
112*c87b03e5Sespie   _Unwind_Ptr load_base;
113*c87b03e5Sespie   const unsigned char *p;
114*c87b03e5Sespie   const struct unw_eh_frame_hdr *hdr;
115*c87b03e5Sespie   _Unwind_Ptr eh_frame;
116*c87b03e5Sespie   struct object ob;
117*c87b03e5Sespie 
118*c87b03e5Sespie   /* Make sure struct dl_phdr_info is at least as big as we need.  */
119*c87b03e5Sespie   if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
120*c87b03e5Sespie 	     + sizeof (info->dlpi_phnum))
121*c87b03e5Sespie     return -1;
122*c87b03e5Sespie 
123*c87b03e5Sespie   match = 0;
124*c87b03e5Sespie   phdr = info->dlpi_phdr;
125*c87b03e5Sespie   load_base = info->dlpi_addr;
126*c87b03e5Sespie   p_eh_frame_hdr = NULL;
127*c87b03e5Sespie   p_dynamic = NULL;
128*c87b03e5Sespie 
129*c87b03e5Sespie   /* See if PC falls into one of the loaded segments.  Find the eh_frame
130*c87b03e5Sespie      segment at the same time.  */
131*c87b03e5Sespie   for (n = info->dlpi_phnum; --n >= 0; phdr++)
132*c87b03e5Sespie     {
133*c87b03e5Sespie       if (phdr->p_type == PT_LOAD)
134*c87b03e5Sespie 	{
135*c87b03e5Sespie 	  _Unwind_Ptr vaddr = phdr->p_vaddr + load_base;
136*c87b03e5Sespie 	  if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
137*c87b03e5Sespie 	    match = 1;
138*c87b03e5Sespie 	}
139*c87b03e5Sespie       else if (phdr->p_type == PT_GNU_EH_FRAME)
140*c87b03e5Sespie 	p_eh_frame_hdr = phdr;
141*c87b03e5Sespie       else if (phdr->p_type == PT_DYNAMIC)
142*c87b03e5Sespie 	p_dynamic = phdr;
143*c87b03e5Sespie     }
144*c87b03e5Sespie   if (!match || !p_eh_frame_hdr)
145*c87b03e5Sespie     return 0;
146*c87b03e5Sespie 
147*c87b03e5Sespie   /* Read .eh_frame_hdr header.  */
148*c87b03e5Sespie   hdr = (const struct unw_eh_frame_hdr *)
149*c87b03e5Sespie 	(p_eh_frame_hdr->p_vaddr + load_base);
150*c87b03e5Sespie   if (hdr->version != 1)
151*c87b03e5Sespie     return 1;
152*c87b03e5Sespie 
153*c87b03e5Sespie #ifdef CRT_GET_RFIB_DATA
154*c87b03e5Sespie # ifdef __i386__
155*c87b03e5Sespie   data->dbase = NULL;
156*c87b03e5Sespie   if (p_dynamic)
157*c87b03e5Sespie     {
158*c87b03e5Sespie       /* For dynamicly linked executables and shared libraries,
159*c87b03e5Sespie 	 DT_PLTGOT is the gp value for that object.  */
160*c87b03e5Sespie       ElfW(Dyn) *dyn = (ElfW(Dyn) *) (p_dynamic->p_vaddr + load_base);
161*c87b03e5Sespie       for (; dyn->d_tag != DT_NULL ; dyn++)
162*c87b03e5Sespie 	if (dyn->d_tag == DT_PLTGOT)
163*c87b03e5Sespie 	  {
164*c87b03e5Sespie 	    /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it.  */
165*c87b03e5Sespie 	    data->dbase = (void *) dyn->d_un.d_ptr;
166*c87b03e5Sespie 	    break;
167*c87b03e5Sespie 	  }
168*c87b03e5Sespie     }
169*c87b03e5Sespie # else
170*c87b03e5Sespie #  error What is DW_EH_PE_datarel base on this platform?
171*c87b03e5Sespie # endif
172*c87b03e5Sespie #endif
173*c87b03e5Sespie #ifdef CRT_GET_RFIB_TEXT
174*c87b03e5Sespie # error What is DW_EH_PE_textrel base on this platform?
175*c87b03e5Sespie #endif
176*c87b03e5Sespie 
177*c87b03e5Sespie   p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
178*c87b03e5Sespie 				    base_from_cb_data (hdr->eh_frame_ptr_enc,
179*c87b03e5Sespie 						       data),
180*c87b03e5Sespie 				    (const unsigned char *) (hdr + 1),
181*c87b03e5Sespie 				    &eh_frame);
182*c87b03e5Sespie 
183*c87b03e5Sespie   /* We require here specific table encoding to speed things up.
184*c87b03e5Sespie      Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
185*c87b03e5Sespie      as base, not the processor specific DW_EH_PE_datarel.  */
186*c87b03e5Sespie   if (hdr->fde_count_enc != DW_EH_PE_omit
187*c87b03e5Sespie       && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
188*c87b03e5Sespie     {
189*c87b03e5Sespie       _Unwind_Ptr fde_count;
190*c87b03e5Sespie 
191*c87b03e5Sespie       p = read_encoded_value_with_base (hdr->fde_count_enc,
192*c87b03e5Sespie 					base_from_cb_data (hdr->fde_count_enc,
193*c87b03e5Sespie 							   data),
194*c87b03e5Sespie 					p, &fde_count);
195*c87b03e5Sespie       /* Shouldn't happen.  */
196*c87b03e5Sespie       if (fde_count == 0)
197*c87b03e5Sespie 	return 1;
198*c87b03e5Sespie       if ((((_Unwind_Ptr) p) & 3) == 0)
199*c87b03e5Sespie 	{
200*c87b03e5Sespie 	  struct fde_table {
201*c87b03e5Sespie 	    signed initial_loc __attribute__ ((mode (SI)));
202*c87b03e5Sespie 	    signed fde __attribute__ ((mode (SI)));
203*c87b03e5Sespie 	  };
204*c87b03e5Sespie 	  const struct fde_table *table = (const struct fde_table *) p;
205*c87b03e5Sespie 	  size_t lo, hi, mid;
206*c87b03e5Sespie 	  _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
207*c87b03e5Sespie 	  fde *f;
208*c87b03e5Sespie 	  unsigned int f_enc, f_enc_size;
209*c87b03e5Sespie 	  _Unwind_Ptr range;
210*c87b03e5Sespie 
211*c87b03e5Sespie 	  mid = fde_count - 1;
212*c87b03e5Sespie 	  if (data->pc < table[0].initial_loc + data_base)
213*c87b03e5Sespie 	    return 1;
214*c87b03e5Sespie 	  else if (data->pc < table[mid].initial_loc + data_base)
215*c87b03e5Sespie 	    {
216*c87b03e5Sespie 	      lo = 0;
217*c87b03e5Sespie 	      hi = mid;
218*c87b03e5Sespie 
219*c87b03e5Sespie 	      while (lo < hi)
220*c87b03e5Sespie 		{
221*c87b03e5Sespie 		  mid = (lo + hi) / 2;
222*c87b03e5Sespie 		  if (data->pc < table[mid].initial_loc + data_base)
223*c87b03e5Sespie 		    hi = mid;
224*c87b03e5Sespie 		  else if (data->pc >= table[mid + 1].initial_loc + data_base)
225*c87b03e5Sespie 		    lo = mid + 1;
226*c87b03e5Sespie 		  else
227*c87b03e5Sespie 		    break;
228*c87b03e5Sespie 		}
229*c87b03e5Sespie 
230*c87b03e5Sespie 	      if (lo >= hi)
231*c87b03e5Sespie 		__gxx_abort ();
232*c87b03e5Sespie 	    }
233*c87b03e5Sespie 
234*c87b03e5Sespie 	  f = (fde *) (table[mid].fde + data_base);
235*c87b03e5Sespie 	  f_enc = get_fde_encoding (f);
236*c87b03e5Sespie 	  f_enc_size = size_of_encoded_value (f_enc);
237*c87b03e5Sespie 	  read_encoded_value_with_base (f_enc & 0x0f, 0,
238*c87b03e5Sespie 					&f->pc_begin[f_enc_size], &range);
239*c87b03e5Sespie 	  if (data->pc < table[mid].initial_loc + data_base + range)
240*c87b03e5Sespie 	    data->ret = f;
241*c87b03e5Sespie 	  data->func = (void *) (table[mid].initial_loc + data_base);
242*c87b03e5Sespie 	  return 1;
243*c87b03e5Sespie 	}
244*c87b03e5Sespie     }
245*c87b03e5Sespie 
246*c87b03e5Sespie   /* We have no sorted search table, so need to go the slow way.
247*c87b03e5Sespie      As soon as GLIBC will provide API so to notify that a library has been
248*c87b03e5Sespie      removed, we could cache this (and thus use search_object).  */
249*c87b03e5Sespie   ob.pc_begin = NULL;
250*c87b03e5Sespie   ob.tbase = data->tbase;
251*c87b03e5Sespie   ob.dbase = data->dbase;
252*c87b03e5Sespie   ob.u.single = (fde *) eh_frame;
253*c87b03e5Sespie   ob.s.i = 0;
254*c87b03e5Sespie   ob.s.b.mixed_encoding = 1;  /* Need to assume worst case.  */
255*c87b03e5Sespie   data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
256*c87b03e5Sespie   if (data->ret != NULL)
257*c87b03e5Sespie     {
258*c87b03e5Sespie       unsigned int encoding = get_fde_encoding (data->ret);
259*c87b03e5Sespie       read_encoded_value_with_base (encoding,
260*c87b03e5Sespie 				    base_from_cb_data (encoding, data),
261*c87b03e5Sespie 				    data->ret->pc_begin,
262*c87b03e5Sespie 				    (_Unwind_Ptr *)&data->func);
263*c87b03e5Sespie     }
264*c87b03e5Sespie   return 1;
265*c87b03e5Sespie }
266*c87b03e5Sespie 
267*c87b03e5Sespie fde *
_Unwind_Find_FDE(void * pc,struct dwarf_eh_bases * bases)268*c87b03e5Sespie _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
269*c87b03e5Sespie {
270*c87b03e5Sespie   struct unw_eh_callback_data data;
271*c87b03e5Sespie   fde *ret;
272*c87b03e5Sespie 
273*c87b03e5Sespie   ret = _Unwind_Find_registered_FDE (pc, bases);
274*c87b03e5Sespie   if (ret != NULL)
275*c87b03e5Sespie     return ret;
276*c87b03e5Sespie 
277*c87b03e5Sespie   data.pc = (_Unwind_Ptr) pc;
278*c87b03e5Sespie   data.tbase = NULL;
279*c87b03e5Sespie   data.dbase = NULL;
280*c87b03e5Sespie   data.func = NULL;
281*c87b03e5Sespie   data.ret = NULL;
282*c87b03e5Sespie 
283*c87b03e5Sespie   if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
284*c87b03e5Sespie     return NULL;
285*c87b03e5Sespie 
286*c87b03e5Sespie   if (data.ret)
287*c87b03e5Sespie     {
288*c87b03e5Sespie       bases->tbase = data.tbase;
289*c87b03e5Sespie       bases->dbase = data.dbase;
290*c87b03e5Sespie       bases->func = data.func;
291*c87b03e5Sespie     }
292*c87b03e5Sespie   return data.ret;
293*c87b03e5Sespie }
294*c87b03e5Sespie 
295*c87b03e5Sespie #else
296*c87b03e5Sespie /* Prevent multiple include of header files.  */
297*c87b03e5Sespie #define _Unwind_Find_FDE _Unwind_Find_FDE
298*c87b03e5Sespie #include "unwind-dw2-fde.c"
299*c87b03e5Sespie #endif
300