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> |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame^] | 34 | #include <linux/futex.h> |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 35 | #include <pthread.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> |
| 44 | |
| 45 | // These were accidentally declared in <unistd.h> because we stupidly used to inline |
| 46 | // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. |
| 47 | extern "C" { |
| 48 | unsigned int __page_size = PAGE_SIZE; |
Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 49 | unsigned int __page_shift = 12; |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). |
| 53 | extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { |
| 54 | return wait4(pid, status, options, rusage); |
| 55 | } |
| 56 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 57 | // TODO: does anything still need this? |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 58 | extern "C" int __open() { |
| 59 | abort(); |
| 60 | } |
| 61 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 62 | // TODO: does anything still need this? |
| 63 | extern "C" void** __get_tls() { |
| 64 | #include "private/__get_tls.h" |
| 65 | return __get_tls(); |
| 66 | } |
| 67 | |
Elliott Hughes | 152b9de | 2014-03-10 15:54:40 -0700 | [diff] [blame] | 68 | // This non-standard function was in our <string.h> for some reason. |
| 69 | extern "C" void memswap(void* m1, void* m2, size_t n) { |
| 70 | char* p = reinterpret_cast<char*>(m1); |
| 71 | char* p_end = p + n; |
| 72 | char* q = reinterpret_cast<char*>(m2); |
| 73 | while (p < p_end) { |
| 74 | char tmp = *p; |
| 75 | *p = *q; |
| 76 | *q = tmp; |
| 77 | p++; |
| 78 | q++; |
| 79 | } |
| 80 | } |
| 81 | |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 82 | extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) { |
| 83 | // This was removed from POSIX.1-2008, and is not implemented on bionic. |
| 84 | // Needed for ABI compatibility with the NDK. |
| 85 | return ENOSYS; |
| 86 | } |
| 87 | |
| 88 | extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { |
| 89 | // This was removed from POSIX.1-2008. |
| 90 | // Needed for ABI compatibility with the NDK. |
| 91 | *stack_addr = (char*)attr->stack_base + attr->stack_size; |
| 92 | return 0; |
| 93 | } |
| 94 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 95 | // Non-standard cruft that should only ever have been in system/core/toolbox. |
| 96 | extern "C" char* strtotimeval(const char* str, struct timeval* ts) { |
| 97 | char* s; |
| 98 | ts->tv_sec = strtoumax(str, &s, 10); |
| 99 | |
| 100 | long fractional_seconds = 0; |
| 101 | if (*s == '.') { |
| 102 | s++; |
| 103 | int count = 0; |
| 104 | |
| 105 | // Read up to 6 digits (microseconds). |
| 106 | while (*s && isdigit(*s)) { |
| 107 | if (++count < 7) { |
| 108 | fractional_seconds = fractional_seconds*10 + (*s - '0'); |
| 109 | } |
| 110 | s++; |
| 111 | } |
| 112 | |
| 113 | for (; count < 6; count++) { |
| 114 | fractional_seconds *= 10; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | ts->tv_usec = fractional_seconds; |
| 119 | return s; |
| 120 | } |
| 121 | |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 122 | static inline int digitval(int ch) { |
| 123 | unsigned d; |
| 124 | |
| 125 | d = (unsigned)(ch - '0'); |
| 126 | if (d < 10) return (int)d; |
| 127 | |
| 128 | d = (unsigned)(ch - 'a'); |
| 129 | if (d < 6) return (int)(d+10); |
| 130 | |
| 131 | d = (unsigned)(ch - 'A'); |
| 132 | if (d < 6) return (int)(d+10); |
| 133 | |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | // This non-standard function was in our <inttypes.h> for some reason. |
| 138 | extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { |
| 139 | const unsigned char* p = (const unsigned char *)nptr; |
| 140 | const unsigned char* end = p + n; |
| 141 | int minus = 0; |
| 142 | uintmax_t v = 0; |
| 143 | int d; |
| 144 | |
| 145 | while (p < end && isspace(*p)) { |
| 146 | p++; |
| 147 | } |
| 148 | |
| 149 | if (p < end) { |
| 150 | char c = p[0]; |
| 151 | if (c == '-' || c == '+') { |
| 152 | minus = (c == '-'); |
| 153 | p++; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (base == 0) { |
| 158 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 159 | p += 2; |
| 160 | base = 16; |
| 161 | } else if (p+1 < end && p[0] == '0') { |
| 162 | p += 1; |
| 163 | base = 8; |
| 164 | } else { |
| 165 | base = 10; |
| 166 | } |
| 167 | } else if (base == 16) { |
| 168 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 169 | p += 2; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | while (p < end && (d = digitval(*p)) >= 0 && d < base) { |
| 174 | v = v*base + d; |
| 175 | p += 1; |
| 176 | } |
| 177 | |
| 178 | if (endptr) { |
| 179 | *endptr = (char*) p; |
| 180 | } |
| 181 | |
| 182 | return minus ? -v : v; |
| 183 | } |
| 184 | |
| 185 | // This non-standard function was in our <inttypes.h> for some reason. |
| 186 | extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { |
| 187 | return (intmax_t) strntoumax(nptr, endptr, base, n); |
| 188 | } |
| 189 | |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 190 | // POSIX calls this dprintf, but LP32 Android had fdprintf instead. |
| 191 | extern "C" int fdprintf(int fd, const char* fmt, ...) { |
| 192 | va_list ap; |
| 193 | va_start(ap, fmt); |
| 194 | int rc = vdprintf(fd, fmt, ap); |
| 195 | va_end(ap); |
| 196 | return rc; |
| 197 | } |
| 198 | |
| 199 | // POSIX calls this vdprintf, but LP32 Android had fdprintf instead. |
| 200 | extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) { |
| 201 | return vdprintf(fd, fmt, ap); |
| 202 | } |
| 203 | |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame^] | 204 | static inline int __futex(volatile void* ftx, int op, int value, const struct timespec* timeout) { |
| 205 | // Our generated syscall assembler sets errno, but our callers (pthread functions) don't want to. |
| 206 | int saved_errno = errno; |
| 207 | if (syscall(__NR_futex, ftx, op, value, timeout) == 0) { |
| 208 | return 0; |
| 209 | } |
| 210 | int result = -errno; |
| 211 | errno = saved_errno; |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | // This used to be in <sys/atomics.h>. |
| 216 | extern "C" int __futex_wake(volatile void* ftx, int count) { |
| 217 | return __futex(ftx, FUTEX_WAKE, count, NULL); |
| 218 | } |
| 219 | |
| 220 | // This used to be in <sys/atomics.h>. |
| 221 | extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) { |
| 222 | return __futex(ftx, FUTEX_WAIT, value, timeout); |
| 223 | } |
| 224 | |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 225 | #endif |