• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 // Copyright (C) 2022 Beken Corporation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <sys/types.h>
17 
18 #define USE_LIBC_INITFINI        0
19 
20 #if USE_LIBC_INITFINI
21 
22 /*
23  * The _init() and _fini() will be called respectively when use __libc_init_array()
24  * and __libc_fnit_array() in libc.a to perform constructor and destructor handling.
25  * The dummy versions of these functions should be provided.
26  */
27 #if 1
_init(void)28 void _init(void)
29 {
30 }
31 
_fini(void)32 void _fini(void)
33 {
34 }
35 #endif
36 
37 #else
38 
39 /* These magic symbols are provided by the linker.  */
40 extern void (*__preinit_array_start []) (void) __attribute__((weak));
41 extern void (*__preinit_array_end []) (void) __attribute__((weak));
42 extern void (*__init_array_start []) (void) __attribute__((weak));
43 extern void (*__init_array_end []) (void) __attribute__((weak));
44 
45 /*
46  * The __libc_init_array()/__libc_fnit_array() function is used to do global
47  * constructor/destructor and can NOT be compilied to generate the code coverage
48  * data. We have the function attribute to be 'no_profile_instrument_function'
49  * to prevent been instrumented for coverage analysis when GCOV=1 is applied.
50  */
51 /* Iterate over all the init routines.  */
52 //void __libc_init_array (void) __attribute__((no_profile_instrument_function));
__libc_init_array(void)53 void __libc_init_array (void)
54 {
55     size_t count;
56     size_t i;
57 
58     count = __preinit_array_end - __preinit_array_start;
59     for (i = 0; i < count; i++)
60         __preinit_array_start[i] ();
61 
62     count = __init_array_end - __init_array_start;
63     for (i = 0; i < count; i++)
64         __init_array_start[i] ();
65 }
66 
67 extern void (*__fini_array_start []) (void) __attribute__((weak));
68 extern void (*__fini_array_end []) (void) __attribute__((weak));
69 
70 /* Run all the cleanup routines.  */
71 void __libc_fini_array (void) __attribute__((no_profile_instrument_function));
__libc_fini_array(void)72 void __libc_fini_array (void)
73 {
74     size_t count;
75     size_t i;
76 
77     count = __fini_array_end - __fini_array_start;
78     for (i = count; i > 0; i--)
79         __fini_array_start[i-1] ();
80 }
81 
82 #endif
83