darwin-crt2.c 5.3 KB

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