#include <stddef.h>
int NXArgc = 0;
const char** NXArgv = NULL;
const char** environ = NULL;
const char* __progname = NULL;
#if __DYNAMIC__
asm(".desc _NXArgc, 0x10");
asm(".desc _NXArgv, 0x10");
asm(".desc _environ, 0x10");
asm(".desc __mh_execute_header, 0x10");
#if defined(__ppc__) || defined(__i386__)
asm(".comm _catch_exception_raise, 4");
asm(".desc _catch_exception_raise, 0x10");
asm(".comm _catch_exception_raise_state, 4");
asm(".desc _catch_exception_raise_state, 0x10");
asm(".comm _catch_exception_raise_state_identity, 4");
asm(".desc _catch_exception_raise_state_identity, 0x10");
asm(".comm _do_mach_notify_dead_name, 4");
asm(".desc _do_mach_notify_dead_name, 0x10");
asm(".comm _do_seqnos_mach_notify_dead_name, 4");
asm(".desc _do_seqnos_mach_notify_dead_name, 0x10");
asm(".comm _do_mach_notify_no_senders, 4");
asm(".desc _do_mach_notify_no_senders, 0x10");
asm(".comm _do_seqnos_mach_notify_no_senders, 4");
asm(".desc _do_seqnos_mach_notify_no_senders, 0x10");
asm(".comm _do_mach_notify_port_deleted, 4");
asm(".desc _do_mach_notify_port_deleted, 0x10");
asm(".comm _do_seqnos_mach_notify_port_deleted, 4");
asm(".desc _do_seqnos_mach_notify_port_deleted, 0x10");
asm(".comm _do_mach_notify_send_once, 4");
asm(".desc _do_mach_notify_send_once, 0x10");
asm(".comm _do_seqnos_mach_notify_send_once, 4");
asm(".desc _do_seqnos_mach_notify_send_once, 0x10");
asm(".comm _clock_alarm_reply, 4");
asm(".desc _clock_alarm_reply, 0x10");
asm(".comm _receive_samples, 4");
asm(".desc _receive_samples, 0x10");
#endif
asm(".desc ___progname, 0x10");
#endif
extern int (*mach_init_routine)(void);
extern int (*_cthread_init_routine)(void);
#if !__DYNAMIC__
asm(".comm __cplus_init, 4");
extern void _cplus_init(void);
#endif
#if __DYNAMIC__ && __ppc__
asm(".comm ___darwin_gcc3_preregister_frame_info, 4");
extern void __darwin_gcc3_preregister_frame_info (void);
static void (*pointer_to__darwin_gcc3_preregister_frame_info)(void) =
__darwin_gcc3_preregister_frame_info;
#endif
extern int main(int argc, const char* argv[], const char* envp[], const char* apple[]);
extern void exit(int status) __attribute__ ((noreturn));
extern int atexit(void (*fcn)(void));
static const char* crt_basename(const char* path);
#if GCRT
extern void moninit(void);
static void _mcleanup(void);
extern void monitor(char *lowpc,char *highpc,char *buf,int bufsiz,int nfunc);
#endif
#if __DYNAMIC__
extern int _dyld_func_lookup(const char *dyld_func_name,unsigned long *address);
extern void __keymgr_dwarf2_register_sections (void);
#endif
#if __DYNAMIC__ && __ppc__
static void _call_objcInit(void);
#endif
extern int errno;
__private_extern__
void
_start(int argc, const char* argv[], const char* envp[])
{
const char** apple;
#if __DYNAMIC__
void (*term)(void);
void (*init)(void);
#endif
NXArgc = argc;
NXArgv = argv;
environ = envp;
__progname = ((argv[0] != NULL) ? crt_basename(argv[0]) : "");
for(apple = envp; *apple != NULL; ++apple) { }
++apple;
if ( mach_init_routine != 0 )
(void) mach_init_routine();
if ( _cthread_init_routine != 0 )
(*_cthread_init_routine)();
#ifdef __DYNAMIC__
__keymgr_dwarf2_register_sections ();
#endif
#if __ppc__ && __DYNAMIC__
if ( *((int *)pointer_to__darwin_gcc3_preregister_frame_info) != 0 )
pointer_to__darwin_gcc3_preregister_frame_info ();
#endif
#if !__DYNAMIC__
if(*((int *)_cplus_init) != 0)
_cplus_init();
#endif
#ifdef __DYNAMIC__
_dyld_func_lookup("__dyld_make_delayed_module_initializer_calls",
(unsigned long *)&init);
init();
#endif
#if __DYNAMIC__ && __ppc__
_call_objcInit();
#endif
#ifdef GCRT
atexit(_mcleanup);
moninit();
#endif
#ifdef __DYNAMIC__
_dyld_func_lookup("__dyld_mod_term_funcs", (unsigned long *)&term);
if ( term != 0 )
atexit(term);
#endif
errno = 0;
exit(main(argc, argv, envp, apple));
}
#if GCRT
static
void
_mcleanup(
void)
{
monitor(0,0,0,0,0);
}
#endif
static
const char *
crt_basename(const char *path)
{
const char *s;
const char *last = path;
for (s = path; *s != '\0'; s++) {
if (*s == '/') last = s+1;
}
return last;
}
#if __DYNAMIC__ && __ppc__
static
int
crt_strbeginswith(const char *s1, const char *s2)
{
int i;
for (i = 0; ; i++) {
if (s2[i] == '\0') return 1;
else if (s1[i] != s2[i]) return 0;
}
}
static
void
_call_objcInit(void)
{
unsigned int i, count;
unsigned int (*_dyld_image_count_fn)(void);
const char *(*_dyld_get_image_name_fn)(unsigned int image_index);
const void *(*_dyld_get_image_header_fn)(unsigned int image_index);
const void *(*NSLookupSymbolInImage_fn)(const void *image, const char *symbolName, unsigned int options);
void *(*NSAddressOfSymbol_fn)(const void *symbol);
_dyld_func_lookup("__dyld_image_count",
(unsigned long *)&_dyld_image_count_fn);
_dyld_func_lookup("__dyld_get_image_name",
(unsigned long *)&_dyld_get_image_name_fn);
_dyld_func_lookup("__dyld_get_image_header",
(unsigned long *)&_dyld_get_image_header_fn);
_dyld_func_lookup("__dyld_NSLookupSymbolInImage",
(unsigned long *)&NSLookupSymbolInImage_fn);
_dyld_func_lookup("__dyld_NSAddressOfSymbol",
(unsigned long *)&NSAddressOfSymbol_fn);
if (!_dyld_image_count_fn) return;
if (!_dyld_get_image_name_fn) return;
if (!_dyld_get_image_header_fn) return;
if (!NSLookupSymbolInImage_fn) return;
if (!NSAddressOfSymbol_fn) return;
count = (*_dyld_image_count_fn)();
for (i = 0; i < count; i++) {
const void *image;
const char *path = (*_dyld_get_image_name_fn)(i);
const char *base = crt_basename(path);
if (!crt_strbeginswith(base, "libobjc")) continue;
if ((image = (*_dyld_get_image_header_fn)(i))) {
const void *symbol;
if ((symbol = (*NSLookupSymbolInImage_fn)(image,"__objcInit",4))) {
void (*_objcInit_fn)(void) =
(void(*)(void))(*NSAddressOfSymbol_fn)(symbol);
if (_objcInit_fn) {
(*_objcInit_fn)();
break;
}
}
}
}
}
#endif