Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | // This file perpetuates the mistakes of the past, but only for 32-bit targets. |
| 30 | #if !defined(__LP64__) |
| 31 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 32 | #include <ctype.h> |
| 33 | #include <inttypes.h> |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 34 | #include <pthread.h> |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 35 | #include <signal.h> |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <sys/resource.h> |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 39 | #include <sys/syscall.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 40 | #include <sys/time.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/wait.h> |
| 43 | #include <unistd.h> |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 44 | #include <wchar.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 45 | |
| 46 | // These were accidentally declared in <unistd.h> because we stupidly used to inline |
| 47 | // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. |
| 48 | extern "C" { |
| 49 | unsigned int __page_size = PAGE_SIZE; |
Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 50 | unsigned int __page_shift = 12; |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). |
| 54 | extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { |
| 55 | return wait4(pid, status, options, rusage); |
| 56 | } |
| 57 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 58 | // TODO: does anything still need this? |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 59 | extern "C" int __open() { |
| 60 | abort(); |
| 61 | } |
| 62 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 63 | // TODO: does anything still need this? |
| 64 | extern "C" void** __get_tls() { |
| 65 | #include "private/__get_tls.h" |
| 66 | return __get_tls(); |
| 67 | } |
| 68 | |
Elliott Hughes | 152b9de | 2014-03-10 15:54:40 -0700 | [diff] [blame] | 69 | // This non-standard function was in our <string.h> for some reason. |
| 70 | extern "C" void memswap(void* m1, void* m2, size_t n) { |
| 71 | char* p = reinterpret_cast<char*>(m1); |
| 72 | char* p_end = p + n; |
| 73 | char* q = reinterpret_cast<char*>(m2); |
| 74 | while (p < p_end) { |
| 75 | char tmp = *p; |
| 76 | *p = *q; |
| 77 | *q = tmp; |
| 78 | p++; |
| 79 | q++; |
| 80 | } |
| 81 | } |
| 82 | |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 83 | extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) { |
| 84 | // This was removed from POSIX.1-2008, and is not implemented on bionic. |
| 85 | // Needed for ABI compatibility with the NDK. |
| 86 | return ENOSYS; |
| 87 | } |
| 88 | |
| 89 | extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { |
| 90 | // This was removed from POSIX.1-2008. |
| 91 | // Needed for ABI compatibility with the NDK. |
| 92 | *stack_addr = (char*)attr->stack_base + attr->stack_size; |
| 93 | return 0; |
| 94 | } |
| 95 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 96 | // Non-standard cruft that should only ever have been in system/core/toolbox. |
| 97 | extern "C" char* strtotimeval(const char* str, struct timeval* ts) { |
| 98 | char* s; |
| 99 | ts->tv_sec = strtoumax(str, &s, 10); |
| 100 | |
| 101 | long fractional_seconds = 0; |
| 102 | if (*s == '.') { |
| 103 | s++; |
| 104 | int count = 0; |
| 105 | |
| 106 | // Read up to 6 digits (microseconds). |
| 107 | while (*s && isdigit(*s)) { |
| 108 | if (++count < 7) { |
| 109 | fractional_seconds = fractional_seconds*10 + (*s - '0'); |
| 110 | } |
| 111 | s++; |
| 112 | } |
| 113 | |
| 114 | for (; count < 6; count++) { |
| 115 | fractional_seconds *= 10; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | ts->tv_usec = fractional_seconds; |
| 120 | return s; |
| 121 | } |
| 122 | |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 123 | static inline int digitval(int ch) { |
| 124 | unsigned d; |
| 125 | |
| 126 | d = (unsigned)(ch - '0'); |
| 127 | if (d < 10) return (int)d; |
| 128 | |
| 129 | d = (unsigned)(ch - 'a'); |
| 130 | if (d < 6) return (int)(d+10); |
| 131 | |
| 132 | d = (unsigned)(ch - 'A'); |
| 133 | if (d < 6) return (int)(d+10); |
| 134 | |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | // This non-standard function was in our <inttypes.h> for some reason. |
| 139 | extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { |
| 140 | const unsigned char* p = (const unsigned char *)nptr; |
| 141 | const unsigned char* end = p + n; |
| 142 | int minus = 0; |
| 143 | uintmax_t v = 0; |
| 144 | int d; |
| 145 | |
| 146 | while (p < end && isspace(*p)) { |
| 147 | p++; |
| 148 | } |
| 149 | |
| 150 | if (p < end) { |
| 151 | char c = p[0]; |
| 152 | if (c == '-' || c == '+') { |
| 153 | minus = (c == '-'); |
| 154 | p++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (base == 0) { |
| 159 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 160 | p += 2; |
| 161 | base = 16; |
| 162 | } else if (p+1 < end && p[0] == '0') { |
| 163 | p += 1; |
| 164 | base = 8; |
| 165 | } else { |
| 166 | base = 10; |
| 167 | } |
| 168 | } else if (base == 16) { |
| 169 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 170 | p += 2; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | while (p < end && (d = digitval(*p)) >= 0 && d < base) { |
| 175 | v = v*base + d; |
| 176 | p += 1; |
| 177 | } |
| 178 | |
| 179 | if (endptr) { |
| 180 | *endptr = (char*) p; |
| 181 | } |
| 182 | |
| 183 | return minus ? -v : v; |
| 184 | } |
| 185 | |
| 186 | // This non-standard function was in our <inttypes.h> for some reason. |
| 187 | extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { |
| 188 | return (intmax_t) strntoumax(nptr, endptr, base, n); |
| 189 | } |
| 190 | |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 191 | // POSIX calls this dprintf, but LP32 Android had fdprintf instead. |
| 192 | extern "C" int fdprintf(int fd, const char* fmt, ...) { |
| 193 | va_list ap; |
| 194 | va_start(ap, fmt); |
| 195 | int rc = vdprintf(fd, fmt, ap); |
| 196 | va_end(ap); |
| 197 | return rc; |
| 198 | } |
| 199 | |
| 200 | // POSIX calls this vdprintf, but LP32 Android had fdprintf instead. |
| 201 | extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) { |
| 202 | return vdprintf(fd, fmt, ap); |
| 203 | } |
| 204 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 205 | #define __futex_wake __real_futex_wake |
| 206 | #define __futex_wait __real_futex_wait |
| 207 | #include "private/bionic_futex.h" |
| 208 | #undef __futex_wake |
| 209 | #undef __futex_wait |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 210 | |
| 211 | // This used to be in <sys/atomics.h>. |
| 212 | extern "C" int __futex_wake(volatile void* ftx, int count) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 213 | return __real_futex_wake(ftx, count); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // This used to be in <sys/atomics.h>. |
| 217 | extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 218 | return __real_futex_wait(ftx, value, timeout); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Anthony King | 0017073 | 2014-05-24 16:47:14 +0000 | [diff] [blame] | 221 | // Unity's libmono uses this. |
| 222 | extern "C" int tkill(pid_t tid, int sig) { |
| 223 | return syscall(__NR_tkill, tid, sig); |
| 224 | } |
| 225 | |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 226 | extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) { |
| 227 | return wcsstr(haystack, needle); |
| 228 | } |
| 229 | |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 230 | // This was removed from POSIX 2008. |
| 231 | extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) { |
| 232 | return signal(signum, handler); |
| 233 | } |
| 234 | |
| 235 | // sysv_signal() was never in POSIX. |
| 236 | extern sighandler_t _signal(int signum, sighandler_t handler, int flags); |
| 237 | extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) { |
| 238 | return _signal(signum, handler, SA_RESETHAND); |
| 239 | } |
| 240 | |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame^] | 241 | // This is a system call that was never in POSIX. Use readdir(3) instead. |
| 242 | extern "C" int getdents(unsigned int fd, struct dirent* dirp, unsigned int count) { |
| 243 | return __getdents64(fd, dirp, count); |
| 244 | } |
| 245 | |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 246 | #endif |