summaryrefslogtreecommitdiff
path: root/libc/Pre_main
blob: f0a5b83fb1ac2c975d2249c9104ada85fdbc2aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

There is support for calling functions before main and from inside the
exit() function.

The exit processing uses the standard 'atexit' and 'on_exit' functions
see the normal man pages.

Execution of code before main is Linux-8086 (actually BCC) specific; the
method works like this:

/**********************/
long global_var_that_needs_init;

#ifdef __AS386_16__
#asm
  loc	1		! Make sure the pointer is in the correct segment
auto_func:		! Label for bcc -M to work.
  .word	_init_vars	! Pointer to the autorun function
  .word no_op		! Space filler cause segs are padded to 4 bytes.
  .text			! So the function after is also in the correct seg.
#endasm
#endif

#ifdef __AS386_32__
#asm
  loc	1		! Make sure the pointer is in the correct segment
auto_func:		! Label for bcc -M to work.
  .long	_init_vars	! Pointer to the autorun function
  .text			! So the function after is also in the correct seg.
#endasm
#endif

static void init_vars()
{
   time(&global_var_that_needs_init);
}
/**********************/

The most important part is that the asm code _MUST_ be compiled into the
same object file as the variable that is being initialised.

If this is not so the init_vars function will not be linked in and will
not be executed.

Also do note that the init functions are called in essentially random order
(It's actually the order that they appear in the executable) so you must be
careful not to call any routines that have their own autostart from inside
your autostart. Nevertheless you should try to ensure that your routines
will fail gracefully if they are called before the initilisation routine.

If you do not know if a routine has an autostart create a test program
calling the routine and compile with:

   bcc -0 [other_stuff] -M test.c | sort +2 | more

Look down the list for lines like this:

                crt0             ZP_safety  1  00000000  R
                crt0            auto_start  1  00000008  R
             objname             auto_func  1  00000008  R      <<****
                crt0            data_start  2  00000010  R
                crt0              _environ  3  00000010  R

The line marked is an autostart function call.

Robert.