1 /*
2  * Copyright (C) 2008 The Android Open Source Project
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  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 // clang interprets -fno-builtin more loosely than you might expect,
30 // and thinks it's okay to still substitute builtins as long as they're
31 // named __aeabi_* rather than __builtin_*, which causes infinite
32 // recursion if we have the fortified memcpy visible in this file.
33 #undef _FORTIFY_SOURCE
34 
35 #include <stddef.h>
36 #include <string.h>
37 
38 extern int __cxa_atexit(void (*)(void*), void*, void*);
39 
40 // All of these are weak symbols to avoid multiple definition errors when
41 // linking with libstdc++-v3 or compiler-rt.
42 
43 /* The "C++ ABI for ARM" document states that static C++ constructors,
44  * which are called from the .init_array, should manually call
45  * __aeabi_atexit() to register static destructors explicitly.
46  *
47  * Note that 'dso_handle' is the address of a magic linker-generate
48  * variable from the shared object that contains the constructor/destructor
49  */
50 
51 int __attribute__((weak))
__aeabi_atexit_impl(void * object,void (* destructor)(void *),void * dso_handle)52 __aeabi_atexit_impl(void *object, void (*destructor) (void *), void *dso_handle) {
53     return __cxa_atexit(destructor, object, dso_handle);
54 }
55 
56 int __attribute__((weak))
__aeabi_atexit_impl2(void * object,void (* destructor)(void *),void * dso_handle)57 __aeabi_atexit_impl2(void *object, void (*destructor) (void *), void *dso_handle) {
58     return __cxa_atexit(destructor, object, dso_handle);
59 }
60 
61 
__aeabi_memcpy8_impl(void * dest,const void * src,size_t n)62 void __attribute__((weak)) __aeabi_memcpy8_impl(void *dest, const void *src, size_t n) {
63     memcpy(dest, src, n);
64 }
65 
__aeabi_memcpy4_impl(void * dest,const void * src,size_t n)66 void __attribute__((weak)) __aeabi_memcpy4_impl(void *dest, const void *src, size_t n) {
67     memcpy(dest, src, n);
68 }
69 
__aeabi_memcpy_impl(void * dest,const void * src,size_t n)70 void __attribute__((weak)) __aeabi_memcpy_impl(void *dest, const void *src, size_t n) {
71     memcpy(dest, src, n);
72 }
73 
__aeabi_memcpy8_impl2(void * dest,const void * src,size_t n)74 void __attribute__((weak)) __aeabi_memcpy8_impl2(void *dest, const void *src, size_t n) {
75     memcpy(dest, src, n);
76 }
77 
__aeabi_memcpy4_impl2(void * dest,const void * src,size_t n)78 void __attribute__((weak)) __aeabi_memcpy4_impl2(void *dest, const void *src, size_t n) {
79     memcpy(dest, src, n);
80 }
81 
__aeabi_memcpy_impl2(void * dest,const void * src,size_t n)82 void __attribute__((weak)) __aeabi_memcpy_impl2(void *dest, const void *src, size_t n) {
83     memcpy(dest, src, n);
84 }
85 
86 
__aeabi_memmove8_impl(void * dest,const void * src,size_t n)87 void __attribute__((weak)) __aeabi_memmove8_impl(void *dest, const void *src, size_t n) {
88     memmove(dest, src, n);
89 }
90 
__aeabi_memmove4_impl(void * dest,const void * src,size_t n)91 void __attribute__((weak)) __aeabi_memmove4_impl(void *dest, const void *src, size_t n) {
92     memmove(dest, src, n);
93 }
94 
__aeabi_memmove_impl(void * dest,const void * src,size_t n)95 void __attribute__((weak)) __aeabi_memmove_impl(void *dest, const void *src, size_t n) {
96     memmove(dest, src, n);
97 }
98 
__aeabi_memmove8_impl2(void * dest,const void * src,size_t n)99 void __attribute__((weak)) __aeabi_memmove8_impl2(void *dest, const void *src, size_t n) {
100     memmove(dest, src, n);
101 }
102 
__aeabi_memmove4_impl2(void * dest,const void * src,size_t n)103 void __attribute__((weak)) __aeabi_memmove4_impl2(void *dest, const void *src, size_t n) {
104     memmove(dest, src, n);
105 }
106 
__aeabi_memmove_impl2(void * dest,const void * src,size_t n)107 void __attribute__((weak)) __aeabi_memmove_impl2(void *dest, const void *src, size_t n) {
108     memmove(dest, src, n);
109 }
110 
111 /*
112  * __aeabi_memset has the order of its second and third arguments reversed.
113  *  This allows __aeabi_memclr to tail-call __aeabi_memset
114  */
115 
__aeabi_memset8_impl(void * dest,size_t n,int c)116 void __attribute__((weak)) __aeabi_memset8_impl(void *dest, size_t n, int c) {
117     memset(dest, c, n);
118 }
119 
__aeabi_memset4_impl(void * dest,size_t n,int c)120 void __attribute__((weak)) __aeabi_memset4_impl(void *dest, size_t n, int c) {
121     memset(dest, c, n);
122 }
123 
__aeabi_memset_impl(void * dest,size_t n,int c)124 void __attribute__((weak)) __aeabi_memset_impl(void *dest, size_t n, int c) {
125     memset(dest, c, n);
126 }
127 
__aeabi_memset8_impl2(void * dest,size_t n,int c)128 void __attribute__((weak)) __aeabi_memset8_impl2(void *dest, size_t n, int c) {
129     memset(dest, c, n);
130 }
131 
__aeabi_memset4_impl2(void * dest,size_t n,int c)132 void __attribute__((weak)) __aeabi_memset4_impl2(void *dest, size_t n, int c) {
133     memset(dest, c, n);
134 }
135 
__aeabi_memset_impl2(void * dest,size_t n,int c)136 void __attribute__((weak)) __aeabi_memset_impl2(void *dest, size_t n, int c) {
137     memset(dest, c, n);
138 }
139 
140 
__aeabi_memclr8_impl(void * dest,size_t n)141 void __attribute__((weak)) __aeabi_memclr8_impl(void *dest, size_t n) {
142     __aeabi_memset8_impl(dest, n, 0);
143 }
144 
__aeabi_memclr4_impl(void * dest,size_t n)145 void __attribute__((weak)) __aeabi_memclr4_impl(void *dest, size_t n) {
146     __aeabi_memset4_impl(dest, n, 0);
147 }
148 
__aeabi_memclr_impl(void * dest,size_t n)149 void __attribute__((weak)) __aeabi_memclr_impl(void *dest, size_t n) {
150     __aeabi_memset_impl(dest, n, 0);
151 }
152 
__aeabi_memclr8_impl2(void * dest,size_t n)153 void __attribute__((weak)) __aeabi_memclr8_impl2(void *dest, size_t n) {
154     __aeabi_memset8_impl(dest, n, 0);
155 }
156 
__aeabi_memclr4_impl2(void * dest,size_t n)157 void __attribute__((weak)) __aeabi_memclr4_impl2(void *dest, size_t n) {
158     __aeabi_memset4_impl(dest, n, 0);
159 }
160 
__aeabi_memclr_impl2(void * dest,size_t n)161 void __attribute__((weak)) __aeabi_memclr_impl2(void *dest, size_t n) {
162     __aeabi_memset_impl(dest, n, 0);
163 }
164 
165 #define __AEABI_SYMVERS(fn_name) \
166 __asm__(".symver " #fn_name "_impl, " #fn_name "@@LIBC_N"); \
167 __asm__(".symver " #fn_name "_impl2, " #fn_name "@LIBC_PRIVATE")
168 
169 __AEABI_SYMVERS(__aeabi_atexit);
170 __AEABI_SYMVERS(__aeabi_memcpy8);
171 __AEABI_SYMVERS(__aeabi_memcpy4);
172 __AEABI_SYMVERS(__aeabi_memcpy);
173 __AEABI_SYMVERS(__aeabi_memmove8);
174 __AEABI_SYMVERS(__aeabi_memmove4);
175 __AEABI_SYMVERS(__aeabi_memmove);
176 __AEABI_SYMVERS(__aeabi_memset8);
177 __AEABI_SYMVERS(__aeabi_memset4);
178 __AEABI_SYMVERS(__aeabi_memset);
179 __AEABI_SYMVERS(__aeabi_memclr8);
180 __AEABI_SYMVERS(__aeabi_memclr4);
181 __AEABI_SYMVERS(__aeabi_memclr);
182 
183 #undef __AEABI_SYMVERS
184