1 /* Call ctors and dtors from elinux a.out shared libraries.
2 Copyright (C) 1999, 2000, 2003, 2004, 2005 Axis Communications.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 2. Neither the name of Axis Communications nor the names of its
13 contributors may be used to endorse or promote products derived
14 from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
20 COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE. */
28
29 typedef void (*vfnp) (void);
30
31 /* The guts of the _Libctors and _Libdtors is "optimized" away into
32 empty functions when the definition is visible as well. Simplest
33 solution is to emit the definitions as asm. We have no .previous
34 directive in a.out, so we rely on the fact that everything in this
35 file goes into the .text section. */
36 __asm__
37 (
38 ".text\n\t.global .$global.lib.ctors\n.$global.lib.ctors:\n\t.dword 0"
39 );
40 __asm__
41 (
42 ".text\n\t.global .$global.lib.dtors\n.$global.lib.dtors:\n\t.dword 0"
43 );
44
45 extern vfnp * const _Ctors __asm__(".$global.lib.ctors");
46 extern vfnp * const _Dtors __asm__(".$global.lib.dtors");
47
48 /* We better provide weak empty ctor and dtor lists, since they are
49 not created if the main program does not have ctor/dtors. Because
50 it's otherwise not used, GCC trunk "Mon Jul 25 22:33:14 UTC 2005"
51 thinks it can remove defaultors, so we need to artificially mark it
52 as used. FIXME: Perhaps a GCC bug. */
53
54 static vfnp const defaultors[] __attribute__ ((__used__)) = {0, 0};
55
56 extern vfnp * __CTOR_LIST__ __attribute__ ((weak, alias ("defaultors")));
57 extern vfnp * __DTOR_LIST__ __attribute__ ((weak, alias ("defaultors")));
58
59 void
_Libctors(void)60 _Libctors (void)
61 {
62 const vfnp *firstor = _Ctors;
63 const vfnp *ctors;
64
65 /* Have to find the last ctor; they will run in opposite order as in
66 the table. */
67 if (firstor != 0 && *firstor != 0)
68 {
69 for (ctors = firstor; *ctors != 0; ctors++)
70 ;
71
72 while (--ctors != firstor)
73 {
74 (**ctors)();
75 }
76
77 (**ctors)();
78 }
79 }
80
81 void
_Libdtors(void)82 _Libdtors(void)
83 {
84 const vfnp *dtors = _Dtors;
85
86 if (dtors)
87 while (*dtors != 0)
88 {
89 (**dtors++) ();
90 }
91 }
92