195-6
[darwin-xtools.git] / dyld / src / glue.c
blob9f65555d059b406708411b6756ece73421e4aba7
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@
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.
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@
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>
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
51 void abort()
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;
86 // libc uses assert()
87 void __assert_rtn(const char* func, const char* file, int line, const char* failedexpr)
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();
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, ...)
106 va_list list;
107 va_start(list, format);
108 _simple_vdprintf(STDERR_FILENO, format, list);
109 va_end(list);
110 return 0;
113 // called by LIBC_ABORT
114 void abort_report_np(const char* format, ...)
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);
125 else {
126 // _simple_salloc failed, but at least format may have useful info by itself
127 str = format;
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)
139 int* ep = __error();
140 *ep = 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...
153 void exit(int x)
155 _ZN4dyld4haltEPKc("exit()");
158 // static initializers make calls to __cxa_atexit
159 void __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) {
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;
187 if ( __stack_chk_guard != 0 )
188 return;
192 #if __LP64__
193 __stack_chk_guard = ((long)arc4random() << 32) | arc4random();
194 #else
195 __stack_chk_guard = arc4random();
196 #endif
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
218 void __chk_fail()
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
243 #if !__ppc__
244 FILE* __stderrp = NULL;
245 FILE* __stdoutp = NULL;
246 #endif
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, ...)
254 va_list list;
255 va_start(list, format);
256 _simple_vdprintf(STDERR_FILENO, format, list);
257 va_end(list);
260 void __cxa_bad_typeid()
262 _ZN4dyld4haltEPKc("__cxa_bad_typeid()");