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@
32 #include <mach/mach_error.h>
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
);
43 // dyld::log(const char* format, ...)
44 extern void _ZN4dyld3logEPKcz(const char*, ...);
46 // dyld::halt(const char* msg);
47 extern void _ZN4dyld4haltEPKc(const char* msg
) __attribute__((noreturn
));
50 // abort called by C++ unwinding code
53 _ZN4dyld4haltEPKc("dyld calling abort()\n");
56 // std::terminate called by C++ unwinding code
57 void _ZSt9terminatev()
59 _ZN4dyld4haltEPKc("dyld std::terminate()\n");
62 // std::unexpected called by C++ unwinding code
63 void _ZSt10unexpectedv()
65 _ZN4dyld4haltEPKc("dyld std::unexpected()\n");
68 // __cxxabiv1::__terminate(void (*)()) called to terminate process
69 void _ZN10__cxxabiv111__terminateEPFvvE()
71 _ZN4dyld4haltEPKc("dyld std::__terminate()\n");
74 // __cxxabiv1::__unexpected(void (*)()) called to terminate process
75 void _ZN10__cxxabiv112__unexpectedEPFvvE()
77 _ZN4dyld4haltEPKc("dyld std::__unexpected()\n");
80 // __cxxabiv1::__terminate_handler
81 void* _ZN10__cxxabiv119__terminate_handlerE
= &_ZSt9terminatev
;
83 // __cxxabiv1::__unexpected_handler
84 void* _ZN10__cxxabiv120__unexpected_handlerE
= &_ZSt10unexpectedv
;
87 void __assert_rtn(const char* func
, const char* file
, int line
, const char* failedexpr
)
90 _ZN4dyld3logEPKcz("Assertion failed: (%s), file %s, line %d.\n", failedexpr
, file
, line
);
92 _ZN4dyld3logEPKcz("Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr
, func
, file
, line
);
97 // called by libuwind code before aborting
98 size_t fwrite(const void* ptr
, size_t size
, size_t nitme
, FILE* stream
)
100 return fprintf(stream
, "%s", (char*)ptr
);
103 // called by libuwind code before aborting
104 int fprintf(FILE* file
, const char* format
, ...)
107 va_start(list
, format
);
108 _simple_vdprintf(STDERR_FILENO
, format
, list
);
113 // called by LIBC_ABORT
114 void abort_report_np(const char* format
, ...)
118 _SIMPLE_STRING s
= _simple_salloc();
120 va_start(list
, format
);
121 _simple_vsprintf(s
, format
, list
);
123 str
= _simple_string(s
);
126 // _simple_salloc failed, but at least format may have useful info by itself
129 _ZN4dyld4haltEPKc(str
);
130 // _ZN4dyld4haltEPKc doesn't return, so we can't call _simple_sfree
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
)
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().
147 struct tm
* localtime(const time_t* t
)
149 return (struct tm
*)NULL
;
152 // malloc calls exit(-1) in case of errors...
155 _ZN4dyld4haltEPKc("exit()");
158 // static initializers make calls to __cxa_atexit
161 // do nothing, dyld never terminates
165 // The stack protector routines in lib.c bring in too much stuff, so
166 // make our own custom ones.
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
[])
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
) {
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') )
184 __stack_chk_guard
<<= 4;
185 __stack_chk_guard
|= value
;
187 if ( __stack_chk_guard
!= 0 )
193 __stack_chk_guard
= ((long)arc4random() << 32) | arc4random();
195 __stack_chk_guard
= arc4random();
198 extern void _ZN4dyld4haltEPKc(const char*);
199 void __stack_chk_fail()
201 _ZN4dyld4haltEPKc("stack buffer overrun");
205 // std::_throw_bad_alloc()
206 void _ZSt17__throw_bad_allocv()
208 _ZN4dyld4haltEPKc("__throw_bad_alloc()");
211 // std::_throw_length_error(const char* x)
212 void _ZSt20__throw_length_errorPKc()
214 _ZN4dyld4haltEPKc("_throw_length_error()");
217 // the libc.a version of this drags in ASL
220 _ZN4dyld4haltEPKc("__chk_fail()");
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;
231 // used by some pthread routines
232 char* mach_error_string(mach_error_t err
)
234 return (char *)"unknown error code";
236 char* mach_error_type(mach_error_t err
)
238 return (char *)"(unknown/unknown)";
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
244 FILE* __stderrp
= NULL
;
245 FILE* __stdoutp
= NULL
;
248 // work with c++abi.a
249 void (*__cxa_terminate_handler
)() = _ZSt9terminatev
;
250 void (*__cxa_unexpected_handler
)() = _ZSt10unexpectedv
;
252 void abort_message(const char* format
, ...)
255 va_start(list
, format
);
256 _simple_vdprintf(STDERR_FILENO
, format
, list
);
260 void __cxa_bad_typeid()
262 _ZN4dyld4haltEPKc("__cxa_bad_typeid()");