1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
33 #include <mach/mach_error.h>
35 // from _simple.h in libc
36 typedef struct _SIMPLE
* _SIMPLE_STRING
;
37 extern void _simple_vdprintf(int __fd
, const char *__fmt
, va_list __ap
);
38 extern void _simple_dprintf(int __fd
, const char *__fmt
, ...);
39 extern _SIMPLE_STRING
_simple_salloc(void);
40 extern int _simple_vsprintf(_SIMPLE_STRING __b
, const char *__fmt
, va_list __ap
);
41 extern void _simple_sfree(_SIMPLE_STRING __b
);
42 extern char * _simple_string(_SIMPLE_STRING __b
);
44 // dyld::log(const char* format, ...)
45 extern void _ZN4dyld3logEPKcz(const char*, ...);
47 // dyld::halt(const char* msg);
48 extern void _ZN4dyld4haltEPKc(const char* msg
) __attribute__((noreturn
));
51 // abort called by C++ unwinding code
54 _ZN4dyld4haltEPKc("dyld calling abort()\n");
57 // std::terminate called by C++ unwinding code
58 void _ZSt9terminatev()
60 _ZN4dyld4haltEPKc("dyld std::terminate()\n");
63 // std::unexpected called by C++ unwinding code
64 void _ZSt10unexpectedv()
66 _ZN4dyld4haltEPKc("dyld std::unexpected()\n");
69 // __cxxabiv1::__terminate(void (*)()) called to terminate process
70 void _ZN10__cxxabiv111__terminateEPFvvE()
72 _ZN4dyld4haltEPKc("dyld std::__terminate()\n");
75 // __cxxabiv1::__unexpected(void (*)()) called to terminate process
76 void _ZN10__cxxabiv112__unexpectedEPFvvE()
78 _ZN4dyld4haltEPKc("dyld std::__unexpected()\n");
81 // __cxxabiv1::__terminate_handler
82 void* _ZN10__cxxabiv119__terminate_handlerE
= &_ZSt9terminatev
;
84 // __cxxabiv1::__unexpected_handler
85 void* _ZN10__cxxabiv120__unexpected_handlerE
= &_ZSt10unexpectedv
;
88 void __assert_rtn(const char* func
, const char* file
, int line
, const char* failedexpr
)
91 _ZN4dyld3logEPKcz("Assertion failed: (%s), file %s, line %d.\n", failedexpr
, file
, line
);
93 _ZN4dyld3logEPKcz("Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr
, func
, file
, line
);
98 // called by libuwind code before aborting
99 size_t fwrite(const void* ptr
, size_t size
, size_t nitme
, FILE* stream
)
101 return fprintf(stream
, "%s", (char*)ptr
);
104 // called by libuwind code before aborting
105 int fprintf(FILE* file
, const char* format
, ...)
108 va_start(list
, format
);
109 _simple_vdprintf(STDERR_FILENO
, format
, list
);
114 // called by LIBC_ABORT
115 void abort_report_np(const char* format
, ...)
119 _SIMPLE_STRING s
= _simple_salloc();
121 va_start(list
, format
);
122 _simple_vsprintf(s
, format
, list
);
124 str
= _simple_string(s
);
127 // _simple_salloc failed, but at least format may have useful info by itself
130 _ZN4dyld4haltEPKc(str
);
131 // _ZN4dyld4haltEPKc doesn't return, so we can't call _simple_sfree
135 // real cthread_set_errno_self() has error handling that pulls in
136 // pthread_exit() which pulls in fprintf()
137 extern int* __error(void);
138 void cthread_set_errno_self(int err
)
145 * We have our own localtime() to avoid needing the notify API which is used
146 * by the code in libc.a for localtime() which is used by arc4random().
148 struct tm
* localtime(const time_t* t
)
150 return (struct tm
*)NULL
;
153 // malloc calls exit(-1) in case of errors...
156 _ZN4dyld4haltEPKc("exit()");
159 // static initializers make calls to __cxa_atexit
162 // do nothing, dyld never terminates
166 // The stack protector routines in lib.c bring in too much stuff, so
167 // make our own custom ones.
169 long __stack_chk_guard
= 0;
170 static __attribute__((constructor
))
171 void __guard_setup(int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
173 for (const char** p
= apple
; *p
!= NULL
; ++p
) {
174 if ( strncmp(*p
, "stack_guard=", 12) == 0 ) {
175 // kernel has provide a random value for us
176 for (const char* s
= *p
+ 12; *s
!= '\0'; ++s
) {
179 if ( (c
>= 'a') && (c
<= 'f') )
180 value
= c
- 'a' + 10;
181 else if ( (c
>= 'A') && (c
<= 'F') )
182 value
= c
- 'A' + 10;
183 else if ( (c
>= '0') && (c
<= '9') )
185 __stack_chk_guard
<<= 4;
186 __stack_chk_guard
|= value
;
188 if ( __stack_chk_guard
!= 0 )
194 __stack_chk_guard
= ((long)arc4random() << 32) | arc4random();
196 __stack_chk_guard
= arc4random();
199 extern void _ZN4dyld4haltEPKc(const char*);
200 void __stack_chk_fail()
202 _ZN4dyld4haltEPKc("stack buffer overrun");
206 // std::_throw_bad_alloc()
207 void _ZSt17__throw_bad_allocv()
209 _ZN4dyld4haltEPKc("__throw_bad_alloc()");
212 // std::_throw_length_error(const char* x)
213 void _ZSt20__throw_length_errorPKc()
215 _ZN4dyld4haltEPKc("_throw_length_error()");
218 // the libc.a version of this drags in ASL
221 _ZN4dyld4haltEPKc("__chk_fail()");
225 // referenced by libc.a(pthread.o) but unneeded in dyld
226 void _init_cpu_capabilities() { }
227 void _cpu_capabilities() {}
228 void set_malloc_singlethreaded() {}
229 int PR_5243343_flag
= 0;
232 // used by some pthread routines
233 char* mach_error_string(mach_error_t err
)
235 return (char *)"unknown error code";
237 char* mach_error_type(mach_error_t err
)
239 return (char *)"(unknown/unknown)";
242 // _pthread_reap_thread calls fprintf(stderr).
243 // We map fprint to _simple_vdprintf and ignore FILE* stream, so ok for it to be NULL
244 FILE* __stderrp
= NULL
;
245 FILE* __stdoutp
= NULL
;
247 // work with c++abi.a
248 void (*__cxa_terminate_handler
)() = _ZSt9terminatev
;
249 void (*__cxa_unexpected_handler
)() = _ZSt10unexpectedv
;
251 void abort_message(const char* format
, ...)
254 va_start(list
, format
);
255 _simple_vdprintf(STDERR_FILENO
, format
, list
);
259 void __cxa_bad_typeid()
261 _ZN4dyld4haltEPKc("__cxa_bad_typeid()");
264 // to work with libc++
265 void _ZNKSt3__120__vector_base_commonILb1EE20__throw_length_errorEv()
267 _ZN4dyld4haltEPKc("std::vector<>::_throw_length_error()");
270 // libc.a sometimes missing memset
272 void* memset(void* b
, int c
, size_t len
)
274 uint8_t* p
= (uint8_t*)b
;
275 for(size_t i
=len
; i
> 0; --i
)