Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2004-2010 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <stddef.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <time.h> #include <fcntl.h> #include <unistd.h> #include <stdarg.h> #include <stdio.h> #include <mach/mach.h> #include <mach/mach_time.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <TargetConditionals.h> #include <libkern/OSAtomic.h> #include <errno.h> #include <pthread.h> #if TARGET_IPHONE_SIMULATOR #include "dyldSyscallInterface.h" #endif // from _simple.h in libc typedef struct _SIMPLE* _SIMPLE_STRING; extern void _simple_vdprintf(int __fd, const char *__fmt, va_list __ap); extern void _simple_dprintf(int __fd, const char *__fmt, ...); extern _SIMPLE_STRING _simple_salloc(void); extern int _simple_vsprintf(_SIMPLE_STRING __b, const char *__fmt, va_list __ap); extern void _simple_sfree(_SIMPLE_STRING __b); extern char * _simple_string(_SIMPLE_STRING __b); // dyld::log(const char* format, ...) extern void _ZN4dyld3logEPKcz(const char*, ...); // dyld::halt(const char* msg); extern void _ZN4dyld4haltEPKc(const char* msg) __attribute__((noreturn)); // abort called by C++ unwinding code void abort() { _ZN4dyld4haltEPKc("dyld calling abort()\n"); } // std::terminate called by C++ unwinding code void _ZSt9terminatev() { _ZN4dyld4haltEPKc("dyld std::terminate()\n"); } // std::unexpected called by C++ unwinding code void _ZSt10unexpectedv() { _ZN4dyld4haltEPKc("dyld std::unexpected()\n"); } // __cxxabiv1::__terminate(void (*)()) called to terminate process void _ZN10__cxxabiv111__terminateEPFvvE() { _ZN4dyld4haltEPKc("dyld std::__terminate()\n"); } // __cxxabiv1::__unexpected(void (*)()) called to terminate process void _ZN10__cxxabiv112__unexpectedEPFvvE() { _ZN4dyld4haltEPKc("dyld std::__unexpected()\n"); } // __cxxabiv1::__terminate_handler void* _ZN10__cxxabiv119__terminate_handlerE = &_ZSt9terminatev; // __cxxabiv1::__unexpected_handler void* _ZN10__cxxabiv120__unexpected_handlerE = &_ZSt10unexpectedv; // libc uses assert() void __assert_rtn(const char* func, const char* file, int line, const char* failedexpr) { if (func == NULL) _ZN4dyld3logEPKcz("Assertion failed: (%s), file %s, line %d.\n", failedexpr, file, line); else _ZN4dyld3logEPKcz("Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr, func, file, line); abort(); } // called by libuwind code before aborting size_t fwrite(const void* ptr, size_t size, size_t nitme, FILE* stream) { return fprintf(stream, "%s", (char*)ptr); } // called by libuwind code before aborting int fprintf(FILE* file, const char* format, ...) { va_list list; va_start(list, format); _simple_vdprintf(STDERR_FILENO, format, list); va_end(list); return 0; } // called by LIBC_ABORT void abort_report_np(const char* format, ...) { va_list list; const char *str; _SIMPLE_STRING s = _simple_salloc(); if ( s != NULL ) { va_start(list, format); _simple_vsprintf(s, format, list); va_end(list); str = _simple_string(s); } else { // _simple_salloc failed, but at least format may have useful info by itself str = format; } _ZN4dyld4haltEPKc(str); // _ZN4dyld4haltEPKc doesn't return, so we can't call _simple_sfree } // real cthread_set_errno_self() has error handling that pulls in // pthread_exit() which pulls in fprintf() extern int* __error(void); void cthread_set_errno_self(int err) { int* ep = __error(); *ep = err; } /* * We have our own localtime() to avoid needing the notify API which is used * by the code in libc.a for localtime() which is used by arc4random(). */ struct tm* localtime(const time_t* t) { return (struct tm*)NULL; } // malloc calls exit(-1) in case of errors... void exit(int x) { _ZN4dyld4haltEPKc("exit()"); } // static initializers make calls to __cxa_atexit void __cxa_atexit() { // do nothing, dyld never terminates } // // The stack protector routines in lib.c bring in too much stuff, so // make our own custom ones. // long __stack_chk_guard = 0; void __guard_setup(const char* apple[]) { for (const char** p = apple; *p != NULL; ++p) { if ( strncmp(*p, "stack_guard=", 12) == 0 ) { // kernel has provide a random value for us for (const char* s = *p + 12; *s != '\0'; ++s) { char c = *s; long value = 0; if ( (c >= 'a') && (c <= 'f') ) value = c - 'a' + 10; else if ( (c >= 'A') && (c <= 'F') ) value = c - 'A' + 10; else if ( (c >= '0') && (c <= '9') ) value = c - '0'; __stack_chk_guard <<= 4; __stack_chk_guard |= value; } if ( __stack_chk_guard != 0 ) return; } } #if !TARGET_IPHONE_SIMULATOR #if __LP64__ __stack_chk_guard = ((long)arc4random() << 32) | arc4random(); #else __stack_chk_guard = arc4random(); #endif #endif } extern void _ZN4dyld4haltEPKc(const char*); void __stack_chk_fail() { _ZN4dyld4haltEPKc("stack buffer overrun"); } // std::_throw_bad_alloc() void _ZSt17__throw_bad_allocv() { _ZN4dyld4haltEPKc("__throw_bad_alloc()"); } // std::_throw_length_error(const char* x) void _ZSt20__throw_length_errorPKc() { _ZN4dyld4haltEPKc("_throw_length_error()"); } // the libc.a version of this drags in ASL void __chk_fail() { _ZN4dyld4haltEPKc("__chk_fail()"); } // referenced by libc.a(pthread.o) but unneeded in dyld void _init_cpu_capabilities() { } void _cpu_capabilities() {} void set_malloc_singlethreaded() {} int PR_5243343_flag = 0; // used by some pthread routines char* mach_error_string(mach_error_t err) { return (char *)"unknown error code"; } char* mach_error_type(mach_error_t err) { return (char *)"(unknown/unknown)"; } // _pthread_reap_thread calls fprintf(stderr). // We map fprint to _simple_vdprintf and ignore FILE* stream, so ok for it to be NULL FILE* __stderrp = NULL; FILE* __stdoutp = NULL; // work with c++abi.a void (*__cxa_terminate_handler)() = _ZSt9terminatev; void (*__cxa_unexpected_handler)() = _ZSt10unexpectedv; void abort_message(const char* format, ...) { va_list list; va_start(list, format); _simple_vdprintf(STDERR_FILENO, format, list); va_end(list); } void __cxa_bad_typeid() { _ZN4dyld4haltEPKc("__cxa_bad_typeid()"); } // to work with libc++ void _ZNKSt3__120__vector_base_commonILb1EE20__throw_length_errorEv() { _ZN4dyld4haltEPKc("std::vector<>::_throw_length_error()"); } // libc.a sometimes missing memset #undef memset void* memset(void* b, int c, size_t len) { uint8_t* p = (uint8_t*)b; for(size_t i=len; i > 0; --i) *p++ = c; return b; } // <rdar://problem/10111032> wrap calls to stat() with check for EAGAIN int _ZN4dyld7my_statEPKcP4stat(const char* path, struct stat* buf) { int result; do { result = stat(path, buf); } while ((result == -1) && (errno == EAGAIN)); return result; } // <rdar://problem/13805025> dyld should retry open() if it gets an EGAIN int _ZN4dyld7my_openEPKcii(const char* path, int flag, int other) { int result; do { result = open(path, flag, other); } while ((result == -1) && (errno == EAGAIN)); return result; } // // The dyld in the iOS simulator cannot do syscalls, so it calls back to // host dyld. // #if TARGET_IPHONE_SIMULATOR int myopen(const char* path, int oflag, int extra) __asm("_open"); int myopen(const char* path, int oflag, int extra) { return gSyscallHelpers->open(path, oflag, extra); } int close(int fd) { return gSyscallHelpers->close(fd); } ssize_t pread(int fd, void* buf, size_t nbytes, off_t offset) { return gSyscallHelpers->pread(fd, buf , nbytes, offset); } ssize_t write(int fd, const void *buf, size_t nbytes) { return gSyscallHelpers->write(fd, buf , nbytes); } void* mmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset) { return gSyscallHelpers->mmap(addr, len, prot, flags, fd, offset); } int munmap(void* addr, size_t len) { return gSyscallHelpers->munmap(addr, len); } int madvise(void* addr, size_t len, int advice) { return gSyscallHelpers->madvise(addr, len, advice); } int stat(const char* path, struct stat* buf) { return gSyscallHelpers->stat(path, buf); } int myfcntl(int fd, int cmd, void* result) __asm("_fcntl"); int myfcntl(int fd, int cmd, void* result) { return gSyscallHelpers->fcntl(fd, cmd, result); } int myioctl(int fd, unsigned long request, void* result) __asm("_ioctl"); int myioctl(int fd, unsigned long request, void* result) { return gSyscallHelpers->ioctl(fd, request, result); } int issetugid() { return gSyscallHelpers->issetugid(); } char* getcwd(char* buf, size_t size) { return gSyscallHelpers->getcwd(buf, size); } char* realpath(const char* file_name, char* resolved_name) { return gSyscallHelpers->realpath(file_name, resolved_name); } kern_return_t vm_allocate(vm_map_t target_task, vm_address_t *address, vm_size_t size, int flags) { return gSyscallHelpers->vm_allocate(target_task, address, size, flags); } kern_return_t vm_deallocate(vm_map_t target_task, vm_address_t address, vm_size_t size) { return gSyscallHelpers->vm_deallocate(target_task, address, size); } kern_return_t vm_protect(vm_map_t target_task, vm_address_t address, vm_size_t size, boolean_t max, vm_prot_t prot) { return gSyscallHelpers->vm_protect(target_task, address, size, max, prot); } void _ZN4dyld3logEPKcz(const char* format, ...) { va_list list; va_start(list, format); gSyscallHelpers->vlog(format, list); va_end(list); } void _ZN4dyld4warnEPKcz(const char* format, ...) { va_list list; va_start(list, format); gSyscallHelpers->vwarn(format, list); va_end(list); } int pthread_mutex_lock(pthread_mutex_t* m) { return gSyscallHelpers->pthread_mutex_lock(m); } int pthread_mutex_unlock(pthread_mutex_t* m) { return gSyscallHelpers->pthread_mutex_unlock(m); } mach_port_t mach_thread_self() { return gSyscallHelpers->mach_thread_self(); } kern_return_t mach_port_deallocate(ipc_space_t task, mach_port_name_t name) { return gSyscallHelpers->mach_port_deallocate(task, name); } mach_port_name_t task_self_trap() { return gSyscallHelpers->task_self_trap(); } kern_return_t mach_timebase_info(mach_timebase_info_t info) { return gSyscallHelpers->mach_timebase_info(info); } bool OSAtomicCompareAndSwapPtrBarrier(void* old, void* new, void * volatile *value) { return gSyscallHelpers->OSAtomicCompareAndSwapPtrBarrier(old, new, value); } void OSMemoryBarrier() { return gSyscallHelpers->OSMemoryBarrier(); } uint64_t mach_absolute_time(void) { return gSyscallHelpers->mach_absolute_time(); } int* __error(void) { return gSyscallHelpers->errnoAddress(); } void mach_init() { mach_task_self_ = task_self_trap(); //_task_reply_port = _mach_reply_port(); } mach_port_t mach_task_self_ = MACH_PORT_NULL; extern int myerrno_fallback __asm("_errno"); int myerrno_fallback = 0; #endif // TARGET_IPHONE_SIMULATOR |