home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cplusplus-8 / __main.c next >
Encoding:
C/C++ Source or Header  |  1992-01-20  |  1.8 KB  |  73 lines

  1.  
  2. #include <libc.h>
  3. #include <stdlib.h>
  4. #include <mach-o/loader.h>
  5.  
  6. static void call_static_constructors (void);
  7. static void call_static_destructors (void);
  8. static void call_init_routines (const struct mach_header **headers,
  9.                 const char *segment_name,
  10.                 const char *section_name);
  11.  
  12.  
  13. /* Perform C++ initialization.  This only works for the MH_EXECUTE and
  14.    MH_FVMLIB formats, since the headers must be mapped. */
  15.  
  16. void _cplus_init (void)
  17. {
  18.   atexit (call_static_destructors);
  19.   call_static_constructors ();
  20. }
  21.  
  22.  
  23. /* Call the appropriate contructor for each static object in the program. */
  24.  
  25. static void call_static_constructors (void)
  26. {
  27.   call_init_routines (getmachheaders (), "__TEXT", "__constructor");
  28. }
  29.  
  30.  
  31. /* Call the appropriate destructor for each static object in the program. */
  32.  
  33. static void call_static_destructors (void)
  34. {
  35.   call_init_routines (getmachheaders (), "__TEXT", "__destructor");
  36. }
  37.  
  38.  
  39. /* Call the init functions in the specified section of each header.
  40.    The section is assumed to contain a sequential list of function pointers.
  41.    The functions are called with no arguments, and no return value is
  42.    expected. */
  43.  
  44. static void call_init_routines (const struct mach_header **headers,
  45.                 const char *segment_name,
  46.                 const char *section_name)
  47. {
  48.   unsigned int i;
  49.   
  50.   if (headers == NULL)
  51.     return;
  52.   
  53.   for (i = 0; headers[i] != NULL; i++)
  54.     {
  55.       const struct section *section;
  56.       void (**init_routines) (void);
  57.       unsigned int n, j;
  58.       
  59.       section = getsectbynamefromheader (headers[i],
  60.                      segment_name,
  61.                      section_name);
  62.       
  63.       if (section == NULL)
  64.         continue;
  65.       
  66.       init_routines = (void (**) (void)) section->addr;
  67.       n = section->size / sizeof (init_routines[0]);
  68.       
  69.       for (j = 0; j < n; j++)
  70.     (*init_routines[j]) ();
  71.     }
  72. }
  73.