1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <crt_priv.h>
20 
callVectors(const void * from_addr,const void * to_addr)21 static void callVectors(const void *from_addr, const void *to_addr)
22 {
23     typedef void (* const callVect)(void);
24     callVect *start = (callVect *)from_addr;
25     callVect *end = (callVect *)to_addr;
26     const int32_t step = from_addr < to_addr ? 1 : -1;
27     const int32_t count = step > 0 ? end - start : start - end;
28 
29     // basic sanity check
30     if (&start[step * count] != end)
31         return;
32 
33     for (; start != end; start += step) {
34         callVect vec = *start;
35         if (vec != NULL)
36             vec();
37     }
38 }
39 
__crt_init(void)40 void __crt_init(void)
41 {
42     extern uint32_t __init_array_start[];
43     extern uint32_t __init_array_end[];
44 
45     callVectors(__init_array_start, __init_array_end);
46 }
47 
__crt_exit(void)48 void __crt_exit(void)
49 {
50     extern uint32_t __fini_array_start[];
51     extern uint32_t __fini_array_end[];
52     extern uint32_t __bss_end[];
53     extern uint32_t __got_start[];
54 
55     // call global destructors
56     callVectors(__fini_array_start, __fini_array_end);
57     if (&__fini_array_end[1] <= __got_start) {
58         // call registered static destructors
59         callVectors(__bss_end + __fini_array_end[0] * (sizeof(uint32_t)), __bss_end);
60     }
61 }
62