home *** CD-ROM | disk | FTP | other *** search
-
- #include <libc.h>
- #include <stdlib.h>
- #include <mach-o/loader.h>
-
- static void call_static_constructors (void);
- static void call_static_destructors (void);
- static void call_init_routines (const struct mach_header **headers,
- const char *segment_name,
- const char *section_name);
-
-
- /* Perform C++ initialization. This only works for the MH_EXECUTE and
- MH_FVMLIB formats, since the headers must be mapped. */
-
- void _cplus_init (void)
- {
- atexit (call_static_destructors);
- call_static_constructors ();
- }
-
-
- /* Call the appropriate contructor for each static object in the program. */
-
- static void call_static_constructors (void)
- {
- call_init_routines (getmachheaders (), "__TEXT", "__constructor");
- }
-
-
- /* Call the appropriate destructor for each static object in the program. */
-
- static void call_static_destructors (void)
- {
- call_init_routines (getmachheaders (), "__TEXT", "__destructor");
- }
-
-
- /* Call the init functions in the specified section of each header.
- The section is assumed to contain a sequential list of function pointers.
- The functions are called with no arguments, and no return value is
- expected. */
-
- static void call_init_routines (const struct mach_header **headers,
- const char *segment_name,
- const char *section_name)
- {
- unsigned int i;
-
- if (headers == NULL)
- return;
-
- for (i = 0; headers[i] != NULL; i++)
- {
- const struct section *section;
- void (**init_routines) (void);
- unsigned int n, j;
-
- section = getsectbynamefromheader (headers[i],
- segment_name,
- section_name);
-
- if (section == NULL)
- continue;
-
- init_routines = (void (**) (void)) section->addr;
- n = section->size / sizeof (init_routines[0]);
-
- for (j = 0; j < n; j++)
- (*init_routines[j]) ();
- }
- }
-