Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Copyright 2006, Bernhard Reutner-Fischer |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 6 | */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 7 | #ifndef BB_PLATFORM_H |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 8 | #define BB_PLATFORM_H 1 |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 9 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 10 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 11 | /* Convenience macros to test the version of gcc. */ |
| 12 | #undef __GNUC_PREREQ |
| 13 | #if defined __GNUC__ && defined __GNUC_MINOR__ |
| 14 | # define __GNUC_PREREQ(maj, min) \ |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 15 | ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 16 | #else |
| 17 | # define __GNUC_PREREQ(maj, min) 0 |
| 18 | #endif |
| 19 | |
| 20 | /* __restrict is known in EGCS 1.2 and above. */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 21 | #if !__GNUC_PREREQ(2,92) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 22 | # ifndef __restrict |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 23 | # define __restrict |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 24 | # endif |
| 25 | #endif |
| 26 | |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 27 | #if !__GNUC_PREREQ(2,7) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 28 | # ifndef __attribute__ |
| 29 | # define __attribute__(x) |
| 30 | # endif |
| 31 | #endif |
| 32 | |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 33 | #undef inline |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 34 | #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 35 | /* it's a keyword */ |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 36 | #elif __GNUC_PREREQ(2,7) |
| 37 | # define inline __inline__ |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 38 | #else |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 39 | # define inline |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #ifndef __const |
| 43 | # define __const const |
| 44 | #endif |
| 45 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 46 | #define UNUSED_PARAM __attribute__ ((__unused__)) |
| 47 | #define NORETURN __attribute__ ((__noreturn__)) |
Denys Vlasenko | 53283ad | 2009-11-02 14:20:34 +0100 | [diff] [blame] | 48 | /* "The malloc attribute is used to tell the compiler that a function |
| 49 | * may be treated as if any non-NULL pointer it returns cannot alias |
| 50 | * any other pointer valid when the function returns. This will often |
| 51 | * improve optimization. Standard functions with this property include |
| 52 | * malloc and calloc. realloc-like functions have this property as long |
| 53 | * as the old pointer is never referred to (including comparing it |
| 54 | * to the new pointer) after the function returns a non-NULL value." |
| 55 | */ |
| 56 | #define RETURNS_MALLOC __attribute__ ((malloc)) |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 57 | #define PACKED __attribute__ ((__packed__)) |
| 58 | #define ALIGNED(m) __attribute__ ((__aligned__(m))) |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 59 | |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 60 | /* __NO_INLINE__: some gcc's do not honor inlining! :( */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 61 | #if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__) |
| 62 | # define ALWAYS_INLINE __attribute__ ((always_inline)) inline |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 63 | /* I've seen a toolchain where I needed __noinline__ instead of noinline */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 64 | # define NOINLINE __attribute__((__noinline__)) |
| 65 | # if !ENABLE_WERROR |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 66 | # define DEPRECATED __attribute__ ((__deprecated__)) |
| 67 | # define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result)) |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 68 | # else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 69 | # define DEPRECATED |
| 70 | # define UNUSED_PARAM_RESULT |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 71 | # endif |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 72 | #else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 73 | # define ALWAYS_INLINE inline |
| 74 | # define NOINLINE |
| 75 | # define DEPRECATED |
| 76 | # define UNUSED_PARAM_RESULT |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 77 | #endif |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 78 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 79 | /* -fwhole-program makes all symbols local. The attribute externally_visible |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 80 | * forces a symbol global. */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 81 | #if __GNUC_PREREQ(4,1) |
| 82 | # define EXTERNALLY_VISIBLE __attribute__(( visibility("default") )) |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 83 | //__attribute__ ((__externally_visible__)) |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 84 | #else |
| 85 | # define EXTERNALLY_VISIBLE |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 86 | #endif |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 87 | |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 88 | /* At 4.4 gcc become much more anal about this, need to use "aliased" types */ |
| 89 | #if __GNUC_PREREQ(4,4) |
| 90 | # define FIX_ALIASING __attribute__((__may_alias__)) |
| 91 | #else |
| 92 | # define FIX_ALIASING |
| 93 | #endif |
| 94 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 95 | /* We use __extension__ in some places to suppress -pedantic warnings |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 96 | * about GCC extensions. This feature didn't work properly before |
| 97 | * gcc 2.8. */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 98 | #if !__GNUC_PREREQ(2,8) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 99 | # ifndef __extension__ |
| 100 | # define __extension__ |
| 101 | # endif |
| 102 | #endif |
| 103 | |
Denis Vlasenko | 42b8daf | 2008-06-27 03:55:18 +0000 | [diff] [blame] | 104 | /* FAST_FUNC is a qualifier which (possibly) makes function call faster |
| 105 | * and/or smaller by using modified ABI. It is usually only needed |
| 106 | * on non-static, busybox internal functions. Recent versions of gcc |
| 107 | * optimize statics automatically. FAST_FUNC on static is required |
| 108 | * only if you need to match a function pointer's type */ |
Denis Vlasenko | ac2b50e | 2008-06-27 04:30:48 +0000 | [diff] [blame] | 109 | #if __GNUC_PREREQ(3,0) && defined(i386) /* || defined(__x86_64__)? */ |
| 110 | /* stdcall makes callee to pop arguments from stack, not caller */ |
| 111 | # define FAST_FUNC __attribute__((regparm(3),stdcall)) |
Denis Vlasenko | 42b8daf | 2008-06-27 03:55:18 +0000 | [diff] [blame] | 112 | /* #elif ... - add your favorite arch today! */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 113 | #else |
| 114 | # define FAST_FUNC |
| 115 | #endif |
| 116 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 117 | /* Make all declarations hidden (-fvisibility flag only affects definitions) */ |
| 118 | /* (don't include system headers after this until corresponding pop!) */ |
Denys Vlasenko | 4dc35fb | 2011-07-08 04:41:38 +0200 | [diff] [blame] | 119 | #if __GNUC_PREREQ(4,1) && !defined(__CYGWIN__) |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 120 | # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)") |
| 121 | # define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop") |
| 122 | #else |
| 123 | # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN |
| 124 | # define POP_SAVED_FUNCTION_VISIBILITY |
| 125 | #endif |
| 126 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 127 | /* gcc-2.95 had no va_copy but only __va_copy. */ |
| 128 | #if !__GNUC_PREREQ(3,0) |
| 129 | # include <stdarg.h> |
| 130 | # if !defined va_copy && defined __va_copy |
| 131 | # define va_copy(d,s) __va_copy((d),(s)) |
| 132 | # endif |
| 133 | #endif |
| 134 | |
| 135 | |
Rob Landley | 5cf7c2d | 2006-02-21 06:44:43 +0000 | [diff] [blame] | 136 | /* ---- Endian Detection ------------------------------------ */ |
Rob Landley | 5cf7c2d | 2006-02-21 06:44:43 +0000 | [diff] [blame] | 137 | |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 138 | #include <limits.h> |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 139 | #if defined(__digital__) && defined(__unix__) |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 140 | # include <sex.h> |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 141 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 142 | || defined(__APPLE__) |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 143 | # include <sys/resource.h> /* rlimit */ |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 144 | # include <machine/endian.h> |
| 145 | # define bswap_64 __bswap64 |
| 146 | # define bswap_32 __bswap32 |
| 147 | # define bswap_16 __bswap16 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 148 | #else |
Rob Landley | 15d20a0 | 2006-05-29 05:00:44 +0000 | [diff] [blame] | 149 | # include <byteswap.h> |
| 150 | # include <endian.h> |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 151 | #endif |
| 152 | |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 153 | #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN |
Mike Frysinger | f885513 | 2006-03-28 02:35:56 +0000 | [diff] [blame] | 154 | # define BB_BIG_ENDIAN 1 |
| 155 | # define BB_LITTLE_ENDIAN 0 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 156 | #elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN |
| 157 | # define BB_BIG_ENDIAN 0 |
| 158 | # define BB_LITTLE_ENDIAN 1 |
Waldemar Brodkorb | 95b83ba | 2010-08-06 09:17:26 +0200 | [diff] [blame] | 159 | #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN |
| 160 | # define BB_BIG_ENDIAN 1 |
| 161 | # define BB_LITTLE_ENDIAN 0 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 162 | #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN |
Mike Frysinger | f885513 | 2006-03-28 02:35:56 +0000 | [diff] [blame] | 163 | # define BB_BIG_ENDIAN 0 |
| 164 | # define BB_LITTLE_ENDIAN 1 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 165 | #elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN |
| 166 | # define BB_BIG_ENDIAN 1 |
| 167 | # define BB_LITTLE_ENDIAN 0 |
| 168 | #elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN |
| 169 | # define BB_BIG_ENDIAN 0 |
| 170 | # define BB_LITTLE_ENDIAN 1 |
| 171 | #elif defined(__386__) |
Waldemar Brodkorb | 95b83ba | 2010-08-06 09:17:26 +0200 | [diff] [blame] | 172 | # define BB_BIG_ENDIAN 0 |
| 173 | # define BB_LITTLE_ENDIAN 1 |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 174 | #else |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 175 | # error "Can't determine endianness" |
Rob Landley | 5cf7c2d | 2006-02-21 06:44:43 +0000 | [diff] [blame] | 176 | #endif |
| 177 | |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 178 | #if ULONG_MAX > 0xffffffff |
| 179 | # define bb_bswap_64(x) bswap_64(x) |
| 180 | #endif |
| 181 | |
Denis Vlasenko | 9b0f6e1 | 2008-11-01 13:40:32 +0000 | [diff] [blame] | 182 | /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */ |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 183 | #if BB_BIG_ENDIAN |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 184 | # define SWAP_BE16(x) (x) |
| 185 | # define SWAP_BE32(x) (x) |
| 186 | # define SWAP_BE64(x) (x) |
| 187 | # define SWAP_LE16(x) bswap_16(x) |
| 188 | # define SWAP_LE32(x) bswap_32(x) |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 189 | # define SWAP_LE64(x) bb_bswap_64(x) |
Denys Vlasenko | bcccad3 | 2010-10-16 20:46:35 +0200 | [diff] [blame] | 190 | # define IF_BIG_ENDIAN(...) __VA_ARGS__ |
| 191 | # define IF_LITTLE_ENDIAN(...) |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 192 | #else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 193 | # define SWAP_BE16(x) bswap_16(x) |
| 194 | # define SWAP_BE32(x) bswap_32(x) |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 195 | # define SWAP_BE64(x) bb_bswap_64(x) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 196 | # define SWAP_LE16(x) (x) |
| 197 | # define SWAP_LE32(x) (x) |
| 198 | # define SWAP_LE64(x) (x) |
Denys Vlasenko | bcccad3 | 2010-10-16 20:46:35 +0200 | [diff] [blame] | 199 | # define IF_BIG_ENDIAN(...) |
| 200 | # define IF_LITTLE_ENDIAN(...) __VA_ARGS__ |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 201 | #endif |
| 202 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 203 | |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 204 | /* ---- Unaligned access ------------------------------------ */ |
| 205 | |
Denys Vlasenko | 1f4a987 | 2011-01-22 17:31:35 +0100 | [diff] [blame] | 206 | #include <stdint.h> |
Denys Vlasenko | b40da22 | 2011-01-21 01:16:09 +0100 | [diff] [blame] | 207 | typedef int bb__aliased_int FIX_ALIASING; |
| 208 | typedef uint16_t bb__aliased_uint16_t FIX_ALIASING; |
| 209 | typedef uint32_t bb__aliased_uint32_t FIX_ALIASING; |
| 210 | |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 211 | /* NB: unaligned parameter should be a pointer, aligned one - |
| 212 | * a lvalue. This makes it more likely to not swap them by mistake |
| 213 | */ |
Joakim Tjernlund | 80f4275 | 2010-02-11 08:48:15 +0100 | [diff] [blame] | 214 | #if defined(i386) || defined(__x86_64__) || defined(__powerpc__) |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 215 | # define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp)) |
| 216 | # define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p)) |
| 217 | # define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p)) |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 218 | # define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v)) |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 219 | # define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v)) |
Denis Vlasenko | 42b8daf | 2008-06-27 03:55:18 +0000 | [diff] [blame] | 220 | /* #elif ... - add your favorite arch today! */ |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 221 | #else |
| 222 | /* performs reasonably well (gcc usually inlines memcpy here) */ |
Denys Vlasenko | 57be1ee | 2009-11-26 15:26:31 +0100 | [diff] [blame] | 223 | # define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int))) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 224 | # define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2)) |
| 225 | # define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4)) |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 226 | # define move_to_unaligned16(u16p, v) do { \ |
| 227 | uint16_t __t = (v); \ |
| 228 | memcpy((u16p), &__t, 4); \ |
| 229 | } while (0) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 230 | # define move_to_unaligned32(u32p, v) do { \ |
Denis Vlasenko | 3be2308 | 2009-04-17 22:20:44 +0000 | [diff] [blame] | 231 | uint32_t __t = (v); \ |
| 232 | memcpy((u32p), &__t, 4); \ |
| 233 | } while (0) |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 236 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 237 | /* ---- Size-saving "small" ints (arch-dependent) ----------- */ |
Bernhard Reutner-Fischer | be86209 | 2007-03-19 15:15:06 +0000 | [diff] [blame] | 238 | |
Bernhard Reutner-Fischer | c966ba4 | 2007-01-18 10:32:09 +0000 | [diff] [blame] | 239 | #if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__) |
| 240 | /* add other arches which benefit from this... */ |
| 241 | typedef signed char smallint; |
| 242 | typedef unsigned char smalluint; |
| 243 | #else |
| 244 | /* for arches where byte accesses generate larger code: */ |
| 245 | typedef int smallint; |
| 246 | typedef unsigned smalluint; |
| 247 | #endif |
| 248 | |
Bernhard Reutner-Fischer | a8e2e18 | 2007-01-20 21:27:18 +0000 | [diff] [blame] | 249 | /* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */ |
| 250 | #if (defined __digital__ && defined __unix__) |
| 251 | /* old system without (proper) C99 support */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 252 | # define bool smalluint |
Bernhard Reutner-Fischer | a8e2e18 | 2007-01-20 21:27:18 +0000 | [diff] [blame] | 253 | #else |
| 254 | /* modern system, so use it */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 255 | # include <stdbool.h> |
Bernhard Reutner-Fischer | a8e2e18 | 2007-01-20 21:27:18 +0000 | [diff] [blame] | 256 | #endif |
| 257 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 258 | |
| 259 | /*----- Kernel versioning ------------------------------------*/ |
| 260 | |
| 261 | #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) |
| 262 | |
| 263 | |
| 264 | /* ---- Miscellaneous --------------------------------------- */ |
| 265 | |
| 266 | #if defined __GLIBC__ \ |
| 267 | || defined __UCLIBC__ \ |
| 268 | || defined __dietlibc__ \ |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 269 | || defined __BIONIC__ \ |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 270 | || defined _NEWLIB_VERSION |
| 271 | # include <features.h> |
| 272 | #endif |
| 273 | |
| 274 | /* Define bb_setpgrp */ |
| 275 | #if defined(__digital__) && defined(__unix__) |
| 276 | /* use legacy setpgrp(pid_t, pid_t) for now. move to platform.c */ |
| 277 | # define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0) |
| 278 | #else |
| 279 | # define bb_setpgrp() setpgrp() |
| 280 | #endif |
| 281 | |
| 282 | /* fdprintf is more readable, we used it before dprintf was standardized */ |
| 283 | #include <unistd.h> |
| 284 | #define fdprintf dprintf |
| 285 | |
| 286 | /* Useful for defeating gcc's alignment of "char message[]"-like data */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 287 | #if 1 /* if needed: !defined(arch1) && !defined(arch2) */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 288 | # define ALIGN1 __attribute__((aligned(1))) |
| 289 | # define ALIGN2 __attribute__((aligned(2))) |
Denys Vlasenko | 0ecc116 | 2010-04-14 10:14:25 -0700 | [diff] [blame] | 290 | # define ALIGN4 __attribute__((aligned(4))) |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 291 | #else |
| 292 | /* Arches which MUST have 2 or 4 byte alignment for everything are here */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 293 | # define ALIGN1 |
| 294 | # define ALIGN2 |
Denys Vlasenko | 0ecc116 | 2010-04-14 10:14:25 -0700 | [diff] [blame] | 295 | # define ALIGN4 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 296 | #endif |
| 297 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 298 | /* |
Bernhard Reutner-Fischer | 507cd75 | 2006-05-31 10:04:03 +0000 | [diff] [blame] | 299 | * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably. |
| 300 | * For earlier versions there is no reliable way to check if we are building |
Denis Vlasenko | 9e58921 | 2008-01-24 01:33:42 +0000 | [diff] [blame] | 301 | * for a mmu-less system. |
Bernhard Reutner-Fischer | 507cd75 | 2006-05-31 10:04:03 +0000 | [diff] [blame] | 302 | */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 303 | #if ENABLE_NOMMU || \ |
| 304 | (defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \ |
| 305 | __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 306 | # define BB_MMU 0 |
| 307 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 308 | # define USE_FOR_MMU(...) |
Denis Vlasenko | 473dae0 | 2007-04-11 07:04:23 +0000 | [diff] [blame] | 309 | #else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 310 | # define BB_MMU 1 |
| 311 | # define USE_FOR_NOMMU(...) |
| 312 | # define USE_FOR_MMU(...) __VA_ARGS__ |
Bernhard Reutner-Fischer | 507cd75 | 2006-05-31 10:04:03 +0000 | [diff] [blame] | 313 | #endif |
| 314 | |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 315 | #if defined(__digital__) && defined(__unix__) |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 316 | # include <standards.h> |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 317 | # include <inttypes.h> |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 318 | # define PRIu32 "u" |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 319 | # if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET |
| 320 | # define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET) |
| 321 | # endif |
| 322 | # if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY |
| 323 | # define ADJ_FREQUENCY MOD_FREQUENCY |
| 324 | # endif |
| 325 | # if !defined ADJ_TIMECONST && defined MOD_TIMECONST |
| 326 | # define ADJ_TIMECONST MOD_TIMECONST |
| 327 | # endif |
| 328 | # if !defined ADJ_TICK && defined MOD_CLKB |
| 329 | # define ADJ_TICK MOD_CLKB |
| 330 | # endif |
Rob Landley | 18958e9 | 2006-06-13 18:28:33 +0000 | [diff] [blame] | 331 | #endif |
| 332 | |
Denys Vlasenko | 4dc35fb | 2011-07-08 04:41:38 +0200 | [diff] [blame] | 333 | #if defined(__CYGWIN__) |
| 334 | # define MAXSYMLINKS SYMLOOP_MAX |
| 335 | #endif |
| 336 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 337 | |
| 338 | /* ---- Who misses what? ------------------------------------ */ |
| 339 | |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 340 | /* Assume all these functions and header files exist by default. |
| 341 | * Platforms where it is not true will #undef them below. |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 342 | */ |
| 343 | #define HAVE_CLEARENV 1 |
| 344 | #define HAVE_FDATASYNC 1 |
| 345 | #define HAVE_DPRINTF 1 |
| 346 | #define HAVE_MEMRCHR 1 |
| 347 | #define HAVE_MKDTEMP 1 |
| 348 | #define HAVE_PTSNAME_R 1 |
| 349 | #define HAVE_SETBIT 1 |
| 350 | #define HAVE_SIGHANDLER_T 1 |
| 351 | #define HAVE_STPCPY 1 |
| 352 | #define HAVE_STRCASESTR 1 |
| 353 | #define HAVE_STRCHRNUL 1 |
| 354 | #define HAVE_STRSEP 1 |
| 355 | #define HAVE_STRSIGNAL 1 |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 356 | #define HAVE_STRVERSCMP 1 |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 357 | #define HAVE_VASPRINTF 1 |
Dan Fandrich | 75214cf | 2011-06-30 02:59:17 +0200 | [diff] [blame] | 358 | #define HAVE_UNLOCKED_STDIO 1 |
| 359 | #define HAVE_UNLOCKED_LINE_OPS 1 |
Timo Teras | 0a5b310 | 2011-06-29 02:19:58 +0200 | [diff] [blame] | 360 | #define HAVE_GETLINE 1 |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 361 | #define HAVE_XTABS 1 |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 362 | #define HAVE_MNTENT_H 1 |
| 363 | #define HAVE_NET_ETHERNET_H 1 |
| 364 | #define HAVE_SYS_STATFS_H 1 |
| 365 | |
Denys Vlasenko | 1e18a01 | 2011-06-21 17:12:52 +0200 | [diff] [blame] | 366 | #if defined(__UCLIBC_MAJOR__) |
| 367 | # if __UCLIBC_MAJOR__ == 0 \ |
| 368 | && ( __UCLIBC_MINOR__ < 9 \ |
| 369 | || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 31) \ |
| 370 | ) |
| 371 | # undef HAVE_STRVERSCMP |
| 372 | # endif |
| 373 | #endif |
| 374 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 375 | #if defined(__dietlibc__) |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 376 | # undef HAVE_STRCHRNUL |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 377 | #endif |
| 378 | |
| 379 | #if defined(__WATCOMC__) |
Denys Vlasenko | 47061b4 | 2011-04-17 23:14:19 +0200 | [diff] [blame] | 380 | # undef HAVE_DPRINTF |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 381 | # undef HAVE_GETLINE |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 382 | # undef HAVE_MEMRCHR |
| 383 | # undef HAVE_MKDTEMP |
| 384 | # undef HAVE_SETBIT |
Dan Fandrich | dc50676 | 2011-02-12 22:26:57 -0800 | [diff] [blame] | 385 | # undef HAVE_STPCPY |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 386 | # undef HAVE_STRCASESTR |
| 387 | # undef HAVE_STRCHRNUL |
Dan Fandrich | 0635ddd | 2010-06-18 22:36:45 -0700 | [diff] [blame] | 388 | # undef HAVE_STRSEP |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 389 | # undef HAVE_STRSIGNAL |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 390 | # undef HAVE_STRVERSCMP |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 391 | # undef HAVE_VASPRINTF |
Dan Fandrich | 75214cf | 2011-06-30 02:59:17 +0200 | [diff] [blame] | 392 | # undef HAVE_UNLOCKED_STDIO |
| 393 | # undef HAVE_UNLOCKED_LINE_OPS |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 394 | # undef HAVE_NET_ETHERNET_H |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 395 | #endif |
| 396 | |
Denys Vlasenko | 4dc35fb | 2011-07-08 04:41:38 +0200 | [diff] [blame] | 397 | #if defined(__CYGWIN__) |
| 398 | # undef HAVE_CLEARENV |
| 399 | # undef HAVE_FDPRINTF |
| 400 | # undef HAVE_MEMRCHR |
| 401 | # undef HAVE_PTSNAME_R |
| 402 | # undef HAVE_STRVERSCMP |
| 403 | # undef HAVE_UNLOCKED_LINE_OPS |
| 404 | #endif |
| 405 | |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 406 | /* These BSD-derived OSes share many similarities */ |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 407 | #if (defined __digital__ && defined __unix__) \ |
| 408 | || defined __APPLE__ \ |
| 409 | || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ |
| 410 | # undef HAVE_CLEARENV |
| 411 | # undef HAVE_FDATASYNC |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 412 | # undef HAVE_GETLINE |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 413 | # undef HAVE_MNTENT_H |
| 414 | # undef HAVE_PTSNAME_R |
| 415 | # undef HAVE_SYS_STATFS_H |
| 416 | # undef HAVE_SIGHANDLER_T |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 417 | # undef HAVE_STRVERSCMP |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 418 | # undef HAVE_XTABS |
| 419 | # undef HAVE_DPRINTF |
Matthias Andree | 1285437 | 2011-08-28 05:04:07 +0200 | [diff] [blame] | 420 | # undef HAVE_UNLOCKED_STDIO |
| 421 | # undef HAVE_UNLOCKED_LINE_OPS |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 422 | #endif |
| 423 | |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 424 | #if defined(__FreeBSD__) |
| 425 | # undef HAVE_STRCHRNUL |
| 426 | #endif |
| 427 | |
| 428 | #if defined(__NetBSD__) |
| 429 | # define HAVE_GETLINE 1 /* Recent NetBSD versions have getline() */ |
| 430 | #endif |
| 431 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 432 | #if defined(__digital__) && defined(__unix__) |
| 433 | # undef HAVE_STPCPY |
| 434 | #endif |
| 435 | |
Denys Vlasenko | e0894f5 | 2011-09-09 18:00:44 +0200 | [diff] [blame^] | 436 | #if defined(ANDROID) || defined(__ANDROID__) |
Dan Fandrich | 71d7313 | 2011-06-03 20:51:58 +0200 | [diff] [blame] | 437 | # undef HAVE_DPRINTF |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 438 | # undef HAVE_GETLINE |
Dan Fandrich | 71d7313 | 2011-06-03 20:51:58 +0200 | [diff] [blame] | 439 | # undef HAVE_STPCPY |
| 440 | # undef HAVE_STRCHRNUL |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 441 | # undef HAVE_STRVERSCMP |
Dan Fandrich | 75214cf | 2011-06-30 02:59:17 +0200 | [diff] [blame] | 442 | # undef HAVE_UNLOCKED_LINE_OPS |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 443 | # undef HAVE_NET_ETHERNET_H |
Dan Fandrich | 71d7313 | 2011-06-03 20:51:58 +0200 | [diff] [blame] | 444 | #endif |
| 445 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 446 | /* |
| 447 | * Now, define prototypes for all the functions defined in platform.c |
| 448 | * These must come after all the HAVE_* macros are defined (or not) |
| 449 | */ |
| 450 | |
Denys Vlasenko | 47061b4 | 2011-04-17 23:14:19 +0200 | [diff] [blame] | 451 | #ifndef HAVE_DPRINTF |
| 452 | extern int dprintf(int fd, const char *format, ...); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 453 | #endif |
| 454 | |
| 455 | #ifndef HAVE_MEMRCHR |
| 456 | extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC; |
| 457 | #endif |
| 458 | |
| 459 | #ifndef HAVE_MKDTEMP |
| 460 | extern char *mkdtemp(char *template) FAST_FUNC; |
| 461 | #endif |
| 462 | |
| 463 | #ifndef HAVE_SETBIT |
| 464 | # define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7)) |
| 465 | # define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7))) |
| 466 | #endif |
| 467 | |
Chris Rees | 9ad97d5 | 2011-01-20 00:51:52 +0100 | [diff] [blame] | 468 | #ifndef HAVE_SIGHANDLER_T |
| 469 | typedef void (*sighandler_t)(int); |
| 470 | #endif |
| 471 | |
Dan Fandrich | dc50676 | 2011-02-12 22:26:57 -0800 | [diff] [blame] | 472 | #ifndef HAVE_STPCPY |
| 473 | extern char *stpcpy(char *p, const char *to_add) FAST_FUNC; |
| 474 | #endif |
| 475 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 476 | #ifndef HAVE_STRCASESTR |
| 477 | extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC; |
| 478 | #endif |
| 479 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 480 | #ifndef HAVE_STRCHRNUL |
| 481 | extern char *strchrnul(const char *s, int c) FAST_FUNC; |
| 482 | #endif |
| 483 | |
Dan Fandrich | 0635ddd | 2010-06-18 22:36:45 -0700 | [diff] [blame] | 484 | #ifndef HAVE_STRSEP |
| 485 | extern char *strsep(char **stringp, const char *delim) FAST_FUNC; |
| 486 | #endif |
| 487 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 488 | #ifndef HAVE_STRSIGNAL |
| 489 | /* Not exactly the same: instead of "Stopped" it shows "STOP" etc */ |
| 490 | # define strsignal(sig) get_signame(sig) |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 491 | #endif |
| 492 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 493 | #ifndef HAVE_VASPRINTF |
| 494 | extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC; |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 495 | #endif |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 496 | |
Timo Teras | 0a5b310 | 2011-06-29 02:19:58 +0200 | [diff] [blame] | 497 | #ifndef HAVE_GETLINE |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 498 | # include <stdio.h> /* for FILE */ |
| 499 | # include <sys/types.h> /* size_t */ |
Timo Teras | 0a5b310 | 2011-06-29 02:19:58 +0200 | [diff] [blame] | 500 | extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC; |
| 501 | #endif |
| 502 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 503 | #endif |