]> git.saurik.com Git - apple/dyld.git/blob - src/glue.c
9f65555d059b406708411b6756ece73421e4aba7
[apple/dyld.git] / src / glue.c
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <mach/mach_error.h>
33
34 // from _simple.h in libc
35 typedef struct _SIMPLE* _SIMPLE_STRING;
36 extern void _simple_vdprintf(int __fd, const char *__fmt, va_list __ap);
37 extern void _simple_dprintf(int __fd, const char *__fmt, ...);
38 extern _SIMPLE_STRING _simple_salloc(void);
39 extern int _simple_vsprintf(_SIMPLE_STRING __b, const char *__fmt, va_list __ap);
40 extern void _simple_sfree(_SIMPLE_STRING __b);
41 extern char * _simple_string(_SIMPLE_STRING __b);
42
43 // dyld::log(const char* format, ...)
44 extern void _ZN4dyld3logEPKcz(const char*, ...);
45
46 // dyld::halt(const char* msg);
47 extern void _ZN4dyld4haltEPKc(const char* msg) __attribute__((noreturn));
48
49
50 // abort called by C++ unwinding code
51 void abort()
52 {
53 _ZN4dyld4haltEPKc("dyld calling abort()\n");
54 }
55
56 // std::terminate called by C++ unwinding code
57 void _ZSt9terminatev()
58 {
59 _ZN4dyld4haltEPKc("dyld std::terminate()\n");
60 }
61
62 // std::unexpected called by C++ unwinding code
63 void _ZSt10unexpectedv()
64 {
65 _ZN4dyld4haltEPKc("dyld std::unexpected()\n");
66 }
67
68 // __cxxabiv1::__terminate(void (*)()) called to terminate process
69 void _ZN10__cxxabiv111__terminateEPFvvE()
70 {
71 _ZN4dyld4haltEPKc("dyld std::__terminate()\n");
72 }
73
74 // __cxxabiv1::__unexpected(void (*)()) called to terminate process
75 void _ZN10__cxxabiv112__unexpectedEPFvvE()
76 {
77 _ZN4dyld4haltEPKc("dyld std::__unexpected()\n");
78 }
79
80 // __cxxabiv1::__terminate_handler
81 void* _ZN10__cxxabiv119__terminate_handlerE = &_ZSt9terminatev;
82
83 // __cxxabiv1::__unexpected_handler
84 void* _ZN10__cxxabiv120__unexpected_handlerE = &_ZSt10unexpectedv;
85
86 // libc uses assert()
87 void __assert_rtn(const char* func, const char* file, int line, const char* failedexpr)
88 {
89 if (func == NULL)
90 _ZN4dyld3logEPKcz("Assertion failed: (%s), file %s, line %d.\n", failedexpr, file, line);
91 else
92 _ZN4dyld3logEPKcz("Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr, func, file, line);
93 abort();
94 }
95
96
97 // called by libuwind code before aborting
98 size_t fwrite(const void* ptr, size_t size, size_t nitme, FILE* stream)
99 {
100 return fprintf(stream, "%s", (char*)ptr);
101 }
102
103 // called by libuwind code before aborting
104 int fprintf(FILE* file, const char* format, ...)
105 {
106 va_list list;
107 va_start(list, format);
108 _simple_vdprintf(STDERR_FILENO, format, list);
109 va_end(list);
110 return 0;
111 }
112
113 // called by LIBC_ABORT
114 void abort_report_np(const char* format, ...)
115 {
116 va_list list;
117 const char *str;
118 _SIMPLE_STRING s = _simple_salloc();
119 if ( s != NULL ) {
120 va_start(list, format);
121 _simple_vsprintf(s, format, list);
122 va_end(list);
123 str = _simple_string(s);
124 }
125 else {
126 // _simple_salloc failed, but at least format may have useful info by itself
127 str = format;
128 }
129 _ZN4dyld4haltEPKc(str);
130 // _ZN4dyld4haltEPKc doesn't return, so we can't call _simple_sfree
131 }
132
133
134 // real cthread_set_errno_self() has error handling that pulls in
135 // pthread_exit() which pulls in fprintf()
136 extern int* __error(void);
137 void cthread_set_errno_self(int err)
138 {
139 int* ep = __error();
140 *ep = err;
141 }
142
143 /*
144 * We have our own localtime() to avoid needing the notify API which is used
145 * by the code in libc.a for localtime() which is used by arc4random().
146 */
147 struct tm* localtime(const time_t* t)
148 {
149 return (struct tm*)NULL;
150 }
151
152 // malloc calls exit(-1) in case of errors...
153 void exit(int x)
154 {
155 _ZN4dyld4haltEPKc("exit()");
156 }
157
158 // static initializers make calls to __cxa_atexit
159 void __cxa_atexit()
160 {
161 // do nothing, dyld never terminates
162 }
163
164 //
165 // The stack protector routines in lib.c bring in too much stuff, so
166 // make our own custom ones.
167 //
168 long __stack_chk_guard = 0;
169 static __attribute__((constructor))
170 void __guard_setup(int argc, const char* argv[], const char* envp[], const char* apple[])
171 {
172 for (const char** p = apple; *p != NULL; ++p) {
173 if ( strncmp(*p, "stack_guard=", 12) == 0 ) {
174 // kernel has provide a random value for us
175 for (const char* s = *p + 12; *s != '\0'; ++s) {
176 char c = *s;
177 long value = 0;
178 if ( (c >= 'a') && (c <= 'f') )
179 value = c - 'a' + 10;
180 else if ( (c >= 'A') && (c <= 'F') )
181 value = c - 'A' + 10;
182 else if ( (c >= '0') && (c <= '9') )
183 value = c - '0';
184 __stack_chk_guard <<= 4;
185 __stack_chk_guard |= value;
186 }
187 if ( __stack_chk_guard != 0 )
188 return;
189 }
190 }
191
192 #if __LP64__
193 __stack_chk_guard = ((long)arc4random() << 32) | arc4random();
194 #else
195 __stack_chk_guard = arc4random();
196 #endif
197 }
198 extern void _ZN4dyld4haltEPKc(const char*);
199 void __stack_chk_fail()
200 {
201 _ZN4dyld4haltEPKc("stack buffer overrun");
202 }
203
204
205 // std::_throw_bad_alloc()
206 void _ZSt17__throw_bad_allocv()
207 {
208 _ZN4dyld4haltEPKc("__throw_bad_alloc()");
209 }
210
211 // std::_throw_length_error(const char* x)
212 void _ZSt20__throw_length_errorPKc()
213 {
214 _ZN4dyld4haltEPKc("_throw_length_error()");
215 }
216
217 // the libc.a version of this drags in ASL
218 void __chk_fail()
219 {
220 _ZN4dyld4haltEPKc("__chk_fail()");
221 }
222
223
224 // referenced by libc.a(pthread.o) but unneeded in dyld
225 void _init_cpu_capabilities() { }
226 void _cpu_capabilities() {}
227 void set_malloc_singlethreaded() {}
228 int PR_5243343_flag = 0;
229
230
231 // used by some pthread routines
232 char* mach_error_string(mach_error_t err)
233 {
234 return (char *)"unknown error code";
235 }
236 char* mach_error_type(mach_error_t err)
237 {
238 return (char *)"(unknown/unknown)";
239 }
240
241 // _pthread_reap_thread calls fprintf(stderr).
242 // We map fprint to _simple_vdprintf and ignore FILE* stream, so ok for it to be NULL
243 #if !__ppc__
244 FILE* __stderrp = NULL;
245 FILE* __stdoutp = NULL;
246 #endif
247
248 // work with c++abi.a
249 void (*__cxa_terminate_handler)() = _ZSt9terminatev;
250 void (*__cxa_unexpected_handler)() = _ZSt10unexpectedv;
251
252 void abort_message(const char* format, ...)
253 {
254 va_list list;
255 va_start(list, format);
256 _simple_vdprintf(STDERR_FILENO, format, list);
257 va_end(list);
258 }
259
260 void __cxa_bad_typeid()
261 {
262 _ZN4dyld4haltEPKc("__cxa_bad_typeid()");
263 }