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