xref: /openbsd-src/gnu/gcc/gcc/unwind-dw2-fde-darwin.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
2*404b540aSrobert 
3*404b540aSrobert    This file is part of GCC.
4*404b540aSrobert 
5*404b540aSrobert    GCC is free software; you can redistribute it and/or modify
6*404b540aSrobert    it under the terms of the GNU General Public License as published by
7*404b540aSrobert    the Free Software Foundation; either version 2, or (at your option)
8*404b540aSrobert    any later version.
9*404b540aSrobert 
10*404b540aSrobert    GCC is distributed in the hope that it will be useful,
11*404b540aSrobert    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*404b540aSrobert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*404b540aSrobert    GNU General Public License for more details.
14*404b540aSrobert 
15*404b540aSrobert    You should have received a copy of the GNU General Public License
16*404b540aSrobert    along with GCC; see the file COPYING.  If not, write to
17*404b540aSrobert    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
18*404b540aSrobert    Boston, MA 02110-1301, USA.  */
19*404b540aSrobert 
20*404b540aSrobert /* As a special exception, if you link this library with other files,
21*404b540aSrobert    some of which are compiled with GCC, to produce an executable,
22*404b540aSrobert    this library does not by itself cause the resulting executable
23*404b540aSrobert    to be covered by the GNU General Public License.
24*404b540aSrobert    This exception does not however invalidate any other reasons why
25*404b540aSrobert    the executable file might be covered by the GNU General Public License.  */
26*404b540aSrobert 
27*404b540aSrobert /* Locate the FDE entry for a given address, using Darwin's keymgr support.  */
28*404b540aSrobert 
29*404b540aSrobert #include "tconfig.h"
30*404b540aSrobert #include "tsystem.h"
31*404b540aSrobert #include <string.h>
32*404b540aSrobert #include <stdlib.h>
33*404b540aSrobert #include "dwarf2.h"
34*404b540aSrobert #include "unwind.h"
35*404b540aSrobert #define NO_BASE_OF_ENCODED_VALUE
36*404b540aSrobert #define DWARF2_OBJECT_END_PTR_EXTENSION
37*404b540aSrobert #include "unwind-pe.h"
38*404b540aSrobert #include "unwind-dw2-fde.h"
39*404b540aSrobert /* Carefully don't include gthr.h.  */
40*404b540aSrobert 
41*404b540aSrobert typedef int __gthread_mutex_t;
42*404b540aSrobert #define __gthread_mutex_lock(x)  (void)(x)
43*404b540aSrobert #define __gthread_mutex_unlock(x) (void)(x)
44*404b540aSrobert 
45*404b540aSrobert static const fde * _Unwind_Find_registered_FDE (void *pc,
46*404b540aSrobert 						struct dwarf_eh_bases *bases);
47*404b540aSrobert 
48*404b540aSrobert #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
49*404b540aSrobert #include "unwind-dw2-fde.c"
50*404b540aSrobert #undef _Unwind_Find_FDE
51*404b540aSrobert 
52*404b540aSrobert /* KeyMgr stuff.  */
53*404b540aSrobert #define KEYMGR_GCC3_LIVE_IMAGE_LIST     301     /* loaded images  */
54*404b540aSrobert #define KEYMGR_GCC3_DW2_OBJ_LIST        302     /* Dwarf2 object list  */
55*404b540aSrobert 
56*404b540aSrobert extern void *_keymgr_get_and_lock_processwide_ptr (int);
57*404b540aSrobert extern void _keymgr_set_and_unlock_processwide_ptr (int, void *);
58*404b540aSrobert extern void _keymgr_unlock_processwide_ptr (int);
59*404b540aSrobert 
60*404b540aSrobert struct mach_header;
61*404b540aSrobert struct mach_header_64;
62*404b540aSrobert extern char *getsectdatafromheader (struct mach_header*, const char*,
63*404b540aSrobert 				    const char *, unsigned long *);
64*404b540aSrobert extern char *getsectdatafromheader_64 (struct mach_header_64*, const char*,
65*404b540aSrobert 				       const char *, unsigned long *);
66*404b540aSrobert 
67*404b540aSrobert /* This is referenced from KEYMGR_GCC3_DW2_OBJ_LIST.  */
68*404b540aSrobert struct km_object_info {
69*404b540aSrobert   struct object *seen_objects;
70*404b540aSrobert   struct object *unseen_objects;
71*404b540aSrobert   unsigned spare[2];
72*404b540aSrobert };
73*404b540aSrobert 
74*404b540aSrobert /* Node of KEYMGR_GCC3_LIVE_IMAGE_LIST.  Info about each resident image.  */
75*404b540aSrobert struct live_images {
76*404b540aSrobert   unsigned long this_size;                      /* sizeof (live_images)  */
77*404b540aSrobert   struct mach_header *mh;                       /* the image info  */
78*404b540aSrobert   unsigned long vm_slide;
79*404b540aSrobert   void (*destructor)(struct live_images *);     /* destructor for this  */
80*404b540aSrobert   struct live_images *next;
81*404b540aSrobert   unsigned int examined_p;
82*404b540aSrobert   void *fde;
83*404b540aSrobert   void *object_info;
84*404b540aSrobert   unsigned long info[2];                        /* Future use.  */
85*404b540aSrobert };
86*404b540aSrobert 
87*404b540aSrobert /* Bits in the examined_p field of struct live_images.  */
88*404b540aSrobert enum {
89*404b540aSrobert   EXAMINED_IMAGE_MASK = 1,	/* We've seen this one.  */
90*404b540aSrobert   ALLOCED_IMAGE_MASK = 2,	/* The FDE entries were allocated by
91*404b540aSrobert 				   malloc, and must be freed.  This isn't
92*404b540aSrobert 				   used by newer libgcc versions.  */
93*404b540aSrobert   IMAGE_IS_TEXT_MASK = 4,	/* This image is in the TEXT segment.  */
94*404b540aSrobert   DESTRUCTOR_MAY_BE_CALLED_LIVE = 8  /* The destructor may be called on an
95*404b540aSrobert 					object that's part of the live
96*404b540aSrobert 					image list.  */
97*404b540aSrobert };
98*404b540aSrobert 
99*404b540aSrobert /* Delete any data we allocated on a live_images structure.  Either
100*404b540aSrobert    IMAGE has already been removed from the
101*404b540aSrobert    KEYMGR_GCC3_LIVE_IMAGE_LIST and the struct will be deleted
102*404b540aSrobert    after we return, or that list is locked and we're being called
103*404b540aSrobert    because this object might be about to be unloaded.  Called by
104*404b540aSrobert    KeyMgr.  */
105*404b540aSrobert 
106*404b540aSrobert static void
live_image_destructor(struct live_images * image)107*404b540aSrobert live_image_destructor (struct live_images *image)
108*404b540aSrobert {
109*404b540aSrobert   if (image->object_info)
110*404b540aSrobert     {
111*404b540aSrobert       struct km_object_info *the_obj_info;
112*404b540aSrobert 
113*404b540aSrobert       the_obj_info =
114*404b540aSrobert 	_keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
115*404b540aSrobert       if (the_obj_info)
116*404b540aSrobert 	{
117*404b540aSrobert 	  seen_objects = the_obj_info->seen_objects;
118*404b540aSrobert 	  unseen_objects = the_obj_info->unseen_objects;
119*404b540aSrobert 
120*404b540aSrobert 	  /* Free any sorted arrays.  */
121*404b540aSrobert 	  __deregister_frame_info_bases (image->fde);
122*404b540aSrobert 
123*404b540aSrobert 	  the_obj_info->seen_objects = seen_objects;
124*404b540aSrobert 	  the_obj_info->unseen_objects = unseen_objects;
125*404b540aSrobert 	}
126*404b540aSrobert       _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
127*404b540aSrobert 					      the_obj_info);
128*404b540aSrobert 
129*404b540aSrobert       free (image->object_info);
130*404b540aSrobert       image->object_info = NULL;
131*404b540aSrobert       if (image->examined_p & ALLOCED_IMAGE_MASK)
132*404b540aSrobert 	free (image->fde);
133*404b540aSrobert       image->fde = NULL;
134*404b540aSrobert     }
135*404b540aSrobert   image->examined_p = 0;
136*404b540aSrobert   image->destructor = NULL;
137*404b540aSrobert }
138*404b540aSrobert 
139*404b540aSrobert /* Run through the list of live images.  If we can allocate memory,
140*404b540aSrobert    give each unseen image a new `struct object'.  Even if we can't,
141*404b540aSrobert    check whether the PC is inside the FDE of each unseen image.
142*404b540aSrobert  */
143*404b540aSrobert 
144*404b540aSrobert static inline const fde *
examine_objects(void * pc,struct dwarf_eh_bases * bases,int dont_alloc)145*404b540aSrobert examine_objects (void *pc, struct dwarf_eh_bases *bases, int dont_alloc)
146*404b540aSrobert {
147*404b540aSrobert   const fde *result = NULL;
148*404b540aSrobert   struct live_images *image;
149*404b540aSrobert 
150*404b540aSrobert   image = _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
151*404b540aSrobert 
152*404b540aSrobert   for (; image != NULL; image = image->next)
153*404b540aSrobert     if ((image->examined_p & EXAMINED_IMAGE_MASK) == 0)
154*404b540aSrobert       {
155*404b540aSrobert 	char *fde = NULL;
156*404b540aSrobert 	unsigned long sz;
157*404b540aSrobert 
158*404b540aSrobert 	/* For ppc only check whether or not we have __DATA eh frames.  */
159*404b540aSrobert #ifdef __ppc__
160*404b540aSrobert 	fde = getsectdatafromheader (image->mh, "__DATA", "__eh_frame", &sz);
161*404b540aSrobert #endif
162*404b540aSrobert 
163*404b540aSrobert 	if (fde == NULL)
164*404b540aSrobert 	  {
165*404b540aSrobert #if __LP64__
166*404b540aSrobert 	    fde = getsectdatafromheader_64 ((struct mach_header_64 *) image->mh,
167*404b540aSrobert 					    "__TEXT", "__eh_frame", &sz);
168*404b540aSrobert #else
169*404b540aSrobert 	    fde = getsectdatafromheader (image->mh, "__TEXT",
170*404b540aSrobert 					 "__eh_frame", &sz);
171*404b540aSrobert #endif
172*404b540aSrobert 	    if (fde != NULL)
173*404b540aSrobert 	      image->examined_p |= IMAGE_IS_TEXT_MASK;
174*404b540aSrobert 	  }
175*404b540aSrobert 
176*404b540aSrobert 	/* If .eh_frame is empty, don't register at all.  */
177*404b540aSrobert 	if (fde != NULL && sz > 0)
178*404b540aSrobert 	  {
179*404b540aSrobert 	    char *real_fde = (fde + image->vm_slide);
180*404b540aSrobert 	    struct object *ob = NULL;
181*404b540aSrobert 	    struct object panicob;
182*404b540aSrobert 
183*404b540aSrobert 	    if (! dont_alloc)
184*404b540aSrobert 	      ob = calloc (1, sizeof (struct object));
185*404b540aSrobert 	    dont_alloc |= ob == NULL;
186*404b540aSrobert 	    if (dont_alloc)
187*404b540aSrobert 	      ob = &panicob;
188*404b540aSrobert 
189*404b540aSrobert 	    ob->pc_begin = (void *)-1;
190*404b540aSrobert 	    ob->tbase = 0;
191*404b540aSrobert 	    ob->dbase = 0;
192*404b540aSrobert 	    ob->u.single = (struct dwarf_fde *)real_fde;
193*404b540aSrobert 	    ob->s.i = 0;
194*404b540aSrobert 	    ob->s.b.encoding = DW_EH_PE_omit;
195*404b540aSrobert 	    ob->fde_end = real_fde + sz;
196*404b540aSrobert 
197*404b540aSrobert 	    image->fde = real_fde;
198*404b540aSrobert 
199*404b540aSrobert 	    result = search_object (ob, pc);
200*404b540aSrobert 
201*404b540aSrobert 	    if (! dont_alloc)
202*404b540aSrobert 	      {
203*404b540aSrobert 		struct object **p;
204*404b540aSrobert 
205*404b540aSrobert 		image->destructor = live_image_destructor;
206*404b540aSrobert 		image->object_info = ob;
207*404b540aSrobert 
208*404b540aSrobert 		image->examined_p |= (EXAMINED_IMAGE_MASK
209*404b540aSrobert 				      | DESTRUCTOR_MAY_BE_CALLED_LIVE);
210*404b540aSrobert 
211*404b540aSrobert 		/* Insert the object into the classified list.  */
212*404b540aSrobert 		for (p = &seen_objects; *p ; p = &(*p)->next)
213*404b540aSrobert 		  if ((*p)->pc_begin < ob->pc_begin)
214*404b540aSrobert 		    break;
215*404b540aSrobert 		ob->next = *p;
216*404b540aSrobert 		*p = ob;
217*404b540aSrobert 	      }
218*404b540aSrobert 
219*404b540aSrobert 	    if (result)
220*404b540aSrobert 	      {
221*404b540aSrobert 		int encoding;
222*404b540aSrobert 		_Unwind_Ptr func;
223*404b540aSrobert 
224*404b540aSrobert 		bases->tbase = ob->tbase;
225*404b540aSrobert 		bases->dbase = ob->dbase;
226*404b540aSrobert 
227*404b540aSrobert 		encoding = ob->s.b.encoding;
228*404b540aSrobert 		if (ob->s.b.mixed_encoding)
229*404b540aSrobert 		  encoding = get_fde_encoding (result);
230*404b540aSrobert 		read_encoded_value_with_base (encoding,
231*404b540aSrobert 					      base_from_object (encoding, ob),
232*404b540aSrobert 					      result->pc_begin, &func);
233*404b540aSrobert 		bases->func = (void *) func;
234*404b540aSrobert 		break;
235*404b540aSrobert 	      }
236*404b540aSrobert 	  }
237*404b540aSrobert 	else
238*404b540aSrobert 	  image->examined_p |= EXAMINED_IMAGE_MASK;
239*404b540aSrobert       }
240*404b540aSrobert 
241*404b540aSrobert   _keymgr_unlock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
242*404b540aSrobert 
243*404b540aSrobert   return result;
244*404b540aSrobert }
245*404b540aSrobert 
246*404b540aSrobert const fde *
_Unwind_Find_FDE(void * pc,struct dwarf_eh_bases * bases)247*404b540aSrobert _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
248*404b540aSrobert {
249*404b540aSrobert   struct km_object_info *the_obj_info;
250*404b540aSrobert   const fde *ret = NULL;
251*404b540aSrobert 
252*404b540aSrobert   the_obj_info =
253*404b540aSrobert     _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
254*404b540aSrobert   if (! the_obj_info)
255*404b540aSrobert     the_obj_info = calloc (1, sizeof (*the_obj_info));
256*404b540aSrobert 
257*404b540aSrobert   if (the_obj_info != NULL)
258*404b540aSrobert     {
259*404b540aSrobert       seen_objects = the_obj_info->seen_objects;
260*404b540aSrobert       unseen_objects = the_obj_info->unseen_objects;
261*404b540aSrobert 
262*404b540aSrobert       ret = _Unwind_Find_registered_FDE (pc, bases);
263*404b540aSrobert     }
264*404b540aSrobert 
265*404b540aSrobert   /* OK, didn't find it in the list of FDEs we've seen before,
266*404b540aSrobert      so go through and look at the new ones.  */
267*404b540aSrobert   if (ret == NULL)
268*404b540aSrobert     ret = examine_objects (pc, bases, the_obj_info == NULL);
269*404b540aSrobert 
270*404b540aSrobert   if (the_obj_info != NULL)
271*404b540aSrobert     {
272*404b540aSrobert       the_obj_info->seen_objects = seen_objects;
273*404b540aSrobert       the_obj_info->unseen_objects = unseen_objects;
274*404b540aSrobert     }
275*404b540aSrobert   _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
276*404b540aSrobert 					  the_obj_info);
277*404b540aSrobert   return ret;
278*404b540aSrobert }
279