3 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
6 Desc: Common startup code
11 #include <aros/config.h>
14 #include <exec/memory.h>
15 #include <workbench/startup.h>
16 #include <proto/exec.h>
17 #include <proto/dos.h>
18 #include <aros/asmcall.h>
19 #include <aros/debug.h>
20 #include <aros/symbolsets.h>
21 #include <aros/startup.h>
25 THIS_PROGRAM_HANDLES_SYMBOLSETS
27 /* Don't define symbols before the entry point. */
28 extern struct ExecBase
*SysBase
;
29 extern struct WBStartup
*WBenchMsg
;
30 extern int main(int argc
, char ** argv
);
31 int (*__main_function_ptr
)(int argc
, char ** argv
) __attribute__((__weak__
)) = main
;
37 DECLARESET(PROGRAM_ENTRIES
);
40 This won't work for normal AmigaOS because you can't expect SysBase to be
41 in A6. The correct way is to use *(struct ExecBase **)4 and because GCC
42 emits strings for a certain function _before_ the code the program will
43 crash immediately because the first element in the code won't be valid
47 extern char *__argstr
;
48 extern ULONG __argsize
;
51 extern char __stdiowin
[];
52 extern struct DosLibrary
*DOSBase
;
54 static struct aros_startup __aros_startup
;
56 AROS_UFP3(static LONG
, __startup_entry
,
57 AROS_UFHA(char *,argstr
,A0
),
58 AROS_UFHA(ULONG
,argsize
,D0
),
59 AROS_UFHA(struct ExecBase
*,sysbase
,A6
)
60 ) __attribute__((section(".aros.startup")));
62 #warning TODO: reset and initialize the FPU
63 #warning TODO: resident startup
64 AROS_UFH3(static LONG
, __startup_entry
,
65 AROS_UFHA(char *,argstr
,A0
),
66 AROS_UFHA(ULONG
,argsize
,D0
),
67 AROS_UFHA(struct ExecBase
*,sysbase
,A6
)
72 struct Process
*myproc
;
74 BPTR old_in
, old_out
, old_err
;
79 No one program will be able to do anything useful without the dos.library,
80 so we open it here instead of using the automatic opening system
82 DOSBase
= (struct DosLibrary
*)OpenLibrary(DOSNAME
, 39);
83 if (!DOSBase
) return RETURN_FAIL
;
88 myproc
= (struct Process
*)FindTask(NULL
);
90 GetIntETask(myproc
)->iet_startup
= &__aros_startup
;
92 /* Do we have a CLI structure? */
95 /* Workbench startup. Get WBenchMsg and pass it to main() */
97 WaitPort(&myproc
->pr_MsgPort
);
98 WBenchMsg
= (struct WBStartup
*)GetMsg(&myproc
->pr_MsgPort
);
99 __argv
= (char **) WBenchMsg
;
102 D(bug("[startup] Started from Workbench\n"));
104 D(bug("[startup] Opening console window: %s\n", __stdiowin
));
105 win
= Open(__stdiowin
, MODE_OLDFILE
);
107 D(bug("[startup] Success!\n"));
108 old_in
= SelectInput(win
);
109 old_out
= SelectOutput(win
);
110 old_err
= SelectError(win
);
115 __aros_startup
.as_startup_error
= RETURN_FAIL
;
117 if (set_open_libraries())
121 setjmp(__aros_startup
.as_startup_jmp_buf
) == 0 &&
122 set_call_funcs(SETNAME(INIT
), 1, 1)
125 /* ctors/dtors get called in inverse order than init funcs */
126 set_call_funcs(SETNAME(CTORS
), -1, 0);
128 /* Invoke the main function. A weak symbol is used as function name so that
129 it can be overridden (for *nix stuff, for instance). */
130 __aros_startup
.as_startup_error
= (*__main_function_ptr
) (__argc
, __argv
);
132 set_call_funcs(SETNAME(DTORS
), 1, 0);
134 set_call_funcs(SETNAME(EXIT
), -1, 0);
136 set_close_libraries();
139 /* Reply startup message to Workbench */
142 Forbid(); /* make sure we're not UnLoadseg()ed before we're really done */
143 ReplyMsg((struct Message
*) WBenchMsg
);
148 SelectOutput(old_out
);
149 SelectError(old_err
);
152 CloseLibrary((struct Library
*)DOSBase
);
154 return __aros_startup
.as_startup_error
;
159 /* if the programmer hasn't defined a symbol with the name __nocommandline
160 then the code to handle the commandline will be included from the autoinit.lib
162 extern int __nocommandline
;
163 asm(".set __importcommandline, __nocommandline");
165 /* pass these values to the command line handling function */
169 /* the command line handling functions will pass these values back to us */
173 struct ExecBase
*SysBase
;
174 struct DosLibrary
*DOSBase
;
175 struct WBStartup
*WBenchMsg
;
181 DEFINESET(PROGRAM_ENTRIES
);
182 ADD2SET(__startup_entry
, program_entries
, 0);
186 Stub function for GCC __main().
188 The __main() function is originally used for C++ style constructors
189 and destructors in C. This replacement does nothing and gets rid of
190 linker-errors about references to __main().
192 #ifdef AROS_NEEDS___MAIN