1/* KeyMgr backwards-compatibility support for Darwin.
2   Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11In addition to the permissions in the GNU General Public License, the
12Free Software Foundation gives you unlimited permission to link the
13compiled version of this file into combinations with other programs,
14and to distribute those combinations without any restriction coming
15from the use of this file.  (The General Public License restrictions
16do apply in other respects; for example, they cover modification of
17the file, and distribution when not linked into a combine
18executable.)
19
20GCC is distributed in the hope that it will be useful, but WITHOUT ANY
21WARRANTY; without even the implied warranty of MERCHANTABILITY or
22FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23for more details.
24
25You should have received a copy of the GNU General Public License
26along with GCC; see the file COPYING.  If not, write to the Free
27Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2802110-1301, USA.  */
29
30/* It is incorrect to include config.h here, because this file is being
31   compiled for the target, and hence definitions concerning only the host
32   do not apply.  */
33
34#include "tconfig.h"
35#include "tsystem.h"
36
37/* This file doesn't do anything useful on non-powerpc targets, since they
38   don't have backwards compatibility anyway.  */
39
40#ifdef __ppc__
41
42/* Homemade decls substituting for getsect.h and dyld.h, so cross
43   compilation works.  */
44struct mach_header;
45extern char *getsectdatafromheader (struct mach_header *, const char *,
46				    const char *, unsigned long *);
47extern void _dyld_register_func_for_add_image
48  (void (*) (struct mach_header *, unsigned long));
49extern void _dyld_register_func_for_remove_image
50  (void (*) (struct mach_header *, unsigned long));
51
52extern void __darwin_gcc3_preregister_frame_info (void);
53
54/* These are from "keymgr.h".  */
55extern void _init_keymgr (void);
56extern void *_keymgr_get_and_lock_processwide_ptr (unsigned key);
57extern void _keymgr_set_and_unlock_processwide_ptr (unsigned key, void *ptr);
58
59extern void *__keymgr_global[];
60typedef struct _Sinfo_Node {
61        unsigned int size ;             /*size of this node*/
62        unsigned short major_version ;  /*API major version.*/
63        unsigned short minor_version ;  /*API minor version.*/
64        } _Tinfo_Node ;
65
66/* KeyMgr 3.x is the first one supporting GCC3 stuff natively.  */
67#define KEYMGR_API_MAJOR_GCC3           3
68/* ... with these keys.  */
69#define KEYMGR_GCC3_LIVE_IMAGE_LIST	301     /* loaded images  */
70#define KEYMGR_GCC3_DW2_OBJ_LIST	302     /* Dwarf2 object list  */
71
72/* Node of KEYMGR_GCC3_LIVE_IMAGE_LIST.  Info about each resident image.  */
73struct live_images {
74  unsigned long this_size;                      /* sizeof (live_images)  */
75  struct mach_header *mh;                       /* the image info  */
76  unsigned long vm_slide;
77  void (*destructor)(struct live_images *);     /* destructor for this  */
78  struct live_images *next;
79  unsigned int examined_p;
80  void *fde;
81  void *object_info;
82  unsigned long info[2];                        /* Future use.  */
83};
84
85
86/* These routines are used only on Darwin versions before 10.2.
87   Later versions have equivalent code in the system.
88   Eventually, they might go away, although it might be a long time...  */
89
90static void darwin_unwind_dyld_remove_image_hook
91  (struct mach_header *m, unsigned long s);
92static void darwin_unwind_dyld_remove_image_hook
93  (struct mach_header *m, unsigned long s);
94extern void __darwin_gcc3_preregister_frame_info (void);
95
96static void
97darwin_unwind_dyld_add_image_hook (struct mach_header *mh, unsigned long slide)
98{
99  struct live_images *l = (struct live_images *)calloc (1, sizeof (*l));
100  l->mh = mh;
101  l->vm_slide = slide;
102  l->this_size = sizeof (*l);
103  l->next = (struct live_images *)
104	_keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
105  _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST, l);
106}
107
108static void
109darwin_unwind_dyld_remove_image_hook (struct mach_header *m, unsigned long s)
110{
111  struct live_images *top, **lip, *destroy = NULL;
112
113  /* Look for it in the list of live images and delete it.  */
114
115  top = (struct live_images *)
116	   _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
117  for (lip = ⊤ *lip != NULL; lip = &(*lip)->next)
118    {
119      if ((*lip)->mh == m && (*lip)->vm_slide == s)
120        {
121          destroy = *lip;
122          *lip = destroy->next;			/* unlink DESTROY  */
123
124          if (destroy->this_size != sizeof (*destroy))	/* sanity check  */
125            abort ();
126
127          break;
128        }
129    }
130  _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST, top);
131
132  /* Now that we have unlinked this from the image list, toss it.  */
133  if (destroy != NULL)
134    {
135      if (destroy->destructor != NULL)
136	(*destroy->destructor) (destroy);
137      free (destroy);
138    }
139}
140
141void
142__darwin_gcc3_preregister_frame_info (void)
143{
144  const _Tinfo_Node *info;
145  _init_keymgr ();
146  info = (_Tinfo_Node *)__keymgr_global[2];
147  if (info != NULL)
148    {
149      if (info->major_version >= KEYMGR_API_MAJOR_GCC3)
150	return;
151      /* Otherwise, use our own add_image_hooks.  */
152    }
153
154  _dyld_register_func_for_add_image (darwin_unwind_dyld_add_image_hook);
155  _dyld_register_func_for_remove_image (darwin_unwind_dyld_remove_image_hook);
156}
157
158#endif  /* __ppc__ */
159