blob: ea49c7e9251ecef704753616bb2491e4cd730940 [file] [log] [blame]
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Copyright 2006, Bernhard Reutner-Fischer
Denys Vlasenko867ffb92010-08-16 03:24:40 +02004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko867ffb92010-08-16 03:24:40 +02006 */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02007#ifndef BB_PLATFORM_H
Denis Vlasenkof81e8db2009-04-09 12:35:13 +00008#define BB_PLATFORM_H 1
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00009
Dan Fandrich21a542d2009-10-27 11:05:00 +010010
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000011/* 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 Vlasenko9213a9e2006-09-17 16:28:10 +000015 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000016#else
17# define __GNUC_PREREQ(maj, min) 0
18#endif
19
20/* __restrict is known in EGCS 1.2 and above. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000021#if !__GNUC_PREREQ(2,92)
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000022# ifndef __restrict
Denys Vlasenko5a49d282009-05-19 13:18:45 +020023# define __restrict
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000024# endif
25#endif
26
Denis Vlasenko98636eb2008-05-09 17:59:34 +000027#if !__GNUC_PREREQ(2,7)
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000028# ifndef __attribute__
29# define __attribute__(x)
30# endif
31#endif
32
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000033#undef inline
Denis Vlasenkoa7189f02006-11-17 20:29:00 +000034#if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000035/* it's a keyword */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020036#elif __GNUC_PREREQ(2,7)
37# define inline __inline__
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000038#else
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020039# define inline
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000040#endif
41
42#ifndef __const
43# define __const const
44#endif
45
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000046#define UNUSED_PARAM __attribute__ ((__unused__))
47#define NORETURN __attribute__ ((__noreturn__))
Denys Vlasenko619d9b52017-07-28 15:28:33 +020048
49#if __GNUC_PREREQ(4,5)
50# define bb_unreachable(altcode) __builtin_unreachable()
51#else
52# define bb_unreachable(altcode) altcode
53#endif
54
Denys Vlasenko53283ad2009-11-02 14:20:34 +010055/* "The malloc attribute is used to tell the compiler that a function
56 * may be treated as if any non-NULL pointer it returns cannot alias
57 * any other pointer valid when the function returns. This will often
58 * improve optimization. Standard functions with this property include
59 * malloc and calloc. realloc-like functions have this property as long
60 * as the old pointer is never referred to (including comparing it
61 * to the new pointer) after the function returns a non-NULL value."
62 */
63#define RETURNS_MALLOC __attribute__ ((malloc))
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000064#define PACKED __attribute__ ((__packed__))
65#define ALIGNED(m) __attribute__ ((__aligned__(m)))
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020066
Denis Vlasenko0f3a5802008-03-20 13:13:09 +000067/* __NO_INLINE__: some gcc's do not honor inlining! :( */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000068#if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
69# define ALWAYS_INLINE __attribute__ ((always_inline)) inline
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000070/* I've seen a toolchain where I needed __noinline__ instead of noinline */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000071# define NOINLINE __attribute__((__noinline__))
72# if !ENABLE_WERROR
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000073# define DEPRECATED __attribute__ ((__deprecated__))
74# define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000075# else
Denys Vlasenko5a49d282009-05-19 13:18:45 +020076# define DEPRECATED
77# define UNUSED_PARAM_RESULT
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000078# endif
Denis Vlasenko98636eb2008-05-09 17:59:34 +000079#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +020080# define ALWAYS_INLINE inline
81# define NOINLINE
82# define DEPRECATED
83# define UNUSED_PARAM_RESULT
Denis Vlasenko98636eb2008-05-09 17:59:34 +000084#endif
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000085
Bartosz Golaszewskibf0f2c72014-06-26 14:31:05 +020086/* used by unit test machinery to run registration functions before calling main() */
87#define INIT_FUNC __attribute__ ((constructor))
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020088
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000089/* -fwhole-program makes all symbols local. The attribute externally_visible
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +020090 * forces a symbol global. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000091#if __GNUC_PREREQ(4,1)
92# define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000093//__attribute__ ((__externally_visible__))
Denis Vlasenko98636eb2008-05-09 17:59:34 +000094#else
95# define EXTERNALLY_VISIBLE
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +000096#endif
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000097
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010098/* At 4.4 gcc become much more anal about this, need to use "aliased" types */
99#if __GNUC_PREREQ(4,4)
100# define FIX_ALIASING __attribute__((__may_alias__))
101#else
102# define FIX_ALIASING
103#endif
104
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000105/* We use __extension__ in some places to suppress -pedantic warnings
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200106 * about GCC extensions. This feature didn't work properly before
107 * gcc 2.8. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +0000108#if !__GNUC_PREREQ(2,8)
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000109# ifndef __extension__
110# define __extension__
111# endif
112#endif
113
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000114/* FAST_FUNC is a qualifier which (possibly) makes function call faster
115 * and/or smaller by using modified ABI. It is usually only needed
116 * on non-static, busybox internal functions. Recent versions of gcc
117 * optimize statics automatically. FAST_FUNC on static is required
Kang-Che Sungf10f7a22017-01-05 09:25:03 +0800118 * only if you need to match a function pointer's type.
119 * FAST_FUNC may not work well with -flto so allow user to disable this.
120 * (-DFAST_FUNC= )
121 */
122#ifndef FAST_FUNC
123# if __GNUC_PREREQ(3,0) && defined(i386)
Denis Vlasenkoac2b50e2008-06-27 04:30:48 +0000124/* stdcall makes callee to pop arguments from stack, not caller */
Kang-Che Sungf10f7a22017-01-05 09:25:03 +0800125# define FAST_FUNC __attribute__((regparm(3),stdcall))
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000126/* #elif ... - add your favorite arch today! */
Kang-Che Sungf10f7a22017-01-05 09:25:03 +0800127# else
128# define FAST_FUNC
129# endif
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000130#endif
131
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000132/* Make all declarations hidden (-fvisibility flag only affects definitions) */
133/* (don't include system headers after this until corresponding pop!) */
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200134#if __GNUC_PREREQ(4,1) && !defined(__CYGWIN__)
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000135# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)")
136# define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop")
137#else
138# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
139# define POP_SAVED_FUNCTION_VISIBILITY
140#endif
141
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200142/* gcc-2.95 had no va_copy but only __va_copy. */
143#if !__GNUC_PREREQ(3,0)
144# include <stdarg.h>
145# if !defined va_copy && defined __va_copy
146# define va_copy(d,s) __va_copy((d),(s))
147# endif
148#endif
149
150
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000151/* ---- Endian Detection ------------------------------------ */
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000152
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200153#include <limits.h>
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200154#if defined(__digital__) && defined(__unix__)
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +0000155# include <sex.h>
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200156#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
157 || defined(__APPLE__)
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200158# include <sys/resource.h> /* rlimit */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200159# include <machine/endian.h>
160# define bswap_64 __bswap64
161# define bswap_32 __bswap32
162# define bswap_16 __bswap16
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200163#else
Rob Landley15d20a02006-05-29 05:00:44 +0000164# include <byteswap.h>
165# include <endian.h>
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +0000166#endif
167
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200168#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
Mike Frysingerf8855132006-03-28 02:35:56 +0000169# define BB_BIG_ENDIAN 1
170# define BB_LITTLE_ENDIAN 0
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200171#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
172# define BB_BIG_ENDIAN 0
173# define BB_LITTLE_ENDIAN 1
Waldemar Brodkorb95b83ba2010-08-06 09:17:26 +0200174#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
175# define BB_BIG_ENDIAN 1
176# define BB_LITTLE_ENDIAN 0
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200177#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
Mike Frysingerf8855132006-03-28 02:35:56 +0000178# define BB_BIG_ENDIAN 0
179# define BB_LITTLE_ENDIAN 1
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200180#elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
181# define BB_BIG_ENDIAN 1
182# define BB_LITTLE_ENDIAN 0
183#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
184# define BB_BIG_ENDIAN 0
185# define BB_LITTLE_ENDIAN 1
186#elif defined(__386__)
Waldemar Brodkorb95b83ba2010-08-06 09:17:26 +0200187# define BB_BIG_ENDIAN 0
188# define BB_LITTLE_ENDIAN 1
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200189#else
Dan Fandrich21a542d2009-10-27 11:05:00 +0100190# error "Can't determine endianness"
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000191#endif
192
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200193#if ULONG_MAX > 0xffffffff
194# define bb_bswap_64(x) bswap_64(x)
195#endif
196
Denis Vlasenko9b0f6e12008-11-01 13:40:32 +0000197/* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
Rob Landleybba7f082006-05-29 05:51:12 +0000198#if BB_BIG_ENDIAN
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200199# define SWAP_BE16(x) (x)
200# define SWAP_BE32(x) (x)
201# define SWAP_BE64(x) (x)
202# define SWAP_LE16(x) bswap_16(x)
203# define SWAP_LE32(x) bswap_32(x)
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200204# define SWAP_LE64(x) bb_bswap_64(x)
Denys Vlasenkobcccad32010-10-16 20:46:35 +0200205# define IF_BIG_ENDIAN(...) __VA_ARGS__
206# define IF_LITTLE_ENDIAN(...)
Rob Landleybba7f082006-05-29 05:51:12 +0000207#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200208# define SWAP_BE16(x) bswap_16(x)
209# define SWAP_BE32(x) bswap_32(x)
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200210# define SWAP_BE64(x) bb_bswap_64(x)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200211# define SWAP_LE16(x) (x)
212# define SWAP_LE32(x) (x)
213# define SWAP_LE64(x) (x)
Denys Vlasenkobcccad32010-10-16 20:46:35 +0200214# define IF_BIG_ENDIAN(...)
215# define IF_LITTLE_ENDIAN(...) __VA_ARGS__
Rob Landleybba7f082006-05-29 05:51:12 +0000216#endif
217
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200218
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000219/* ---- Unaligned access ------------------------------------ */
220
Denys Vlasenko1f4a9872011-01-22 17:31:35 +0100221#include <stdint.h>
Denys Vlasenkob40da222011-01-21 01:16:09 +0100222typedef int bb__aliased_int FIX_ALIASING;
Lauri Kasanenb8173b62013-01-14 05:20:50 +0100223typedef long bb__aliased_long FIX_ALIASING;
Denys Vlasenkob40da222011-01-21 01:16:09 +0100224typedef uint16_t bb__aliased_uint16_t FIX_ALIASING;
225typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
Denys Vlasenko1f5e81f2013-06-27 01:03:19 +0200226typedef uint64_t bb__aliased_uint64_t FIX_ALIASING;
Denys Vlasenkob40da222011-01-21 01:16:09 +0100227
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000228/* NB: unaligned parameter should be a pointer, aligned one -
229 * a lvalue. This makes it more likely to not swap them by mistake
230 */
Joakim Tjernlund80f42752010-02-11 08:48:15 +0100231#if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
Denys Vlasenkof7f70bf2015-02-02 16:07:07 +0100232# define BB_UNALIGNED_MEMACCESS_OK 1
Lauri Kasanenb8173b62013-01-14 05:20:50 +0100233# define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp))
234# define move_from_unaligned_long(v, longp) ((v) = *(bb__aliased_long*)(longp))
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100235# define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p))
236# define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p))
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100237# define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v))
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100238# define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v))
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000239/* #elif ... - add your favorite arch today! */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000240#else
Denys Vlasenkof7f70bf2015-02-02 16:07:07 +0100241# define BB_UNALIGNED_MEMACCESS_OK 0
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000242/* performs reasonably well (gcc usually inlines memcpy here) */
Denys Vlasenko57be1ee2009-11-26 15:26:31 +0100243# define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
Lauri Kasanenb8173b62013-01-14 05:20:50 +0100244# define move_from_unaligned_long(v, longp) (memcpy(&(v), (longp), sizeof(long)))
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200245# define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
246# define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100247# define move_to_unaligned16(u16p, v) do { \
248 uint16_t __t = (v); \
Denys Vlasenkoe3e32162013-02-27 15:49:38 +0100249 memcpy((u16p), &__t, 2); \
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100250} while (0)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200251# define move_to_unaligned32(u32p, v) do { \
Denis Vlasenko3be23082009-04-17 22:20:44 +0000252 uint32_t __t = (v); \
253 memcpy((u32p), &__t, 4); \
254} while (0)
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000255#endif
256
Denys Vlasenko2c1258c2017-07-15 20:22:25 +0200257/* Unaligned, fixed-endian accessors */
258#define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
259#define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
260#define put_unaligned_le32(val, buf) move_to_unaligned32(buf, SWAP_LE32(val))
261#define put_unaligned_be32(val, buf) move_to_unaligned32(buf, SWAP_BE32(val))
262
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000263
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200264/* ---- Size-saving "small" ints (arch-dependent) ----------- */
Bernhard Reutner-Fischerbe862092007-03-19 15:15:06 +0000265
Bernhard Reutner-Fischerc966ba42007-01-18 10:32:09 +0000266#if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
267/* add other arches which benefit from this... */
268typedef signed char smallint;
269typedef unsigned char smalluint;
270#else
271/* for arches where byte accesses generate larger code: */
272typedef int smallint;
273typedef unsigned smalluint;
274#endif
275
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000276/* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
277#if (defined __digital__ && defined __unix__)
278/* old system without (proper) C99 support */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200279# define bool smalluint
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000280#else
281/* modern system, so use it */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200282# include <stdbool.h>
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000283#endif
284
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200285
286/*----- Kernel versioning ------------------------------------*/
287
288#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
289
Mike Frysinger445e7542013-03-12 11:13:22 -0400290#ifdef __UCLIBC__
291# define UCLIBC_VERSION KERNEL_VERSION(__UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__)
292#else
293# define UCLIBC_VERSION 0
294#endif
295
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200296
297/* ---- Miscellaneous --------------------------------------- */
298
299#if defined __GLIBC__ \
300 || defined __UCLIBC__ \
301 || defined __dietlibc__ \
Denys Vlasenko14bd16a2011-07-08 08:49:40 +0200302 || defined __BIONIC__ \
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200303 || defined _NEWLIB_VERSION
304# include <features.h>
305#endif
306
307/* Define bb_setpgrp */
308#if defined(__digital__) && defined(__unix__)
309/* use legacy setpgrp(pid_t, pid_t) for now. move to platform.c */
310# define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
311#else
312# define bb_setpgrp() setpgrp()
313#endif
314
315/* fdprintf is more readable, we used it before dprintf was standardized */
316#include <unistd.h>
317#define fdprintf dprintf
318
319/* Useful for defeating gcc's alignment of "char message[]"-like data */
Denys Vlasenko2ffd7102012-08-06 17:17:15 +0200320#if !defined(__s390__)
321 /* on s390[x], non-word-aligned data accesses require larger code */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200322# define ALIGN1 __attribute__((aligned(1)))
323# define ALIGN2 __attribute__((aligned(2)))
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700324# define ALIGN4 __attribute__((aligned(4)))
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000325#else
326/* Arches which MUST have 2 or 4 byte alignment for everything are here */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200327# define ALIGN1
328# define ALIGN2
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700329# define ALIGN4
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000330#endif
331
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200332/*
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000333 * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
334 * For earlier versions there is no reliable way to check if we are building
Denis Vlasenko9e589212008-01-24 01:33:42 +0000335 * for a mmu-less system.
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000336 */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +0000337#if ENABLE_NOMMU || \
Mike Frysinger445e7542013-03-12 11:13:22 -0400338 (defined __UCLIBC__ && \
339 UCLIBC_VERSION > KERNEL_VERSION(0, 9, 28) && \
340 !defined __ARCH_USE_MMU__)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200341# define BB_MMU 0
342# define USE_FOR_NOMMU(...) __VA_ARGS__
343# define USE_FOR_MMU(...)
Denis Vlasenko473dae02007-04-11 07:04:23 +0000344#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200345# define BB_MMU 1
346# define USE_FOR_NOMMU(...)
347# define USE_FOR_MMU(...) __VA_ARGS__
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000348#endif
349
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200350#if defined(__digital__) && defined(__unix__)
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000351# include <standards.h>
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000352# include <inttypes.h>
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000353# define PRIu32 "u"
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000354# if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
355# define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
356# endif
357# if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
358# define ADJ_FREQUENCY MOD_FREQUENCY
359# endif
360# if !defined ADJ_TIMECONST && defined MOD_TIMECONST
361# define ADJ_TIMECONST MOD_TIMECONST
362# endif
363# if !defined ADJ_TICK && defined MOD_CLKB
364# define ADJ_TICK MOD_CLKB
365# endif
Rob Landley18958e92006-06-13 18:28:33 +0000366#endif
367
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200368#if defined(__CYGWIN__)
369# define MAXSYMLINKS SYMLOOP_MAX
370#endif
371
Tias Guns36451952012-06-10 14:26:32 +0200372#if defined(ANDROID) || defined(__ANDROID__)
373# define BB_ADDITIONAL_PATH ":/system/sbin:/system/bin:/system/xbin"
374# define SYS_ioprio_set __NR_ioprio_set
375# define SYS_ioprio_get __NR_ioprio_get
376#endif
377
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200378
379/* ---- Who misses what? ------------------------------------ */
380
Dan Fandrichf533ec82011-06-10 05:17:59 +0200381/* Assume all these functions and header files exist by default.
382 * Platforms where it is not true will #undef them below.
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200383 */
384#define HAVE_CLEARENV 1
385#define HAVE_FDATASYNC 1
386#define HAVE_DPRINTF 1
387#define HAVE_MEMRCHR 1
388#define HAVE_MKDTEMP 1
Matt Whitlockcee59052015-04-25 21:32:48 +0200389#define HAVE_TTYNAME_R 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200390#define HAVE_PTSNAME_R 1
391#define HAVE_SETBIT 1
392#define HAVE_SIGHANDLER_T 1
393#define HAVE_STPCPY 1
Denys Vlasenko50a6d862015-01-25 22:08:46 +0100394#define HAVE_MEMPCPY 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200395#define HAVE_STRCASESTR 1
396#define HAVE_STRCHRNUL 1
397#define HAVE_STRSEP 1
398#define HAVE_STRSIGNAL 1
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200399#define HAVE_STRVERSCMP 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200400#define HAVE_VASPRINTF 1
Bernhard Reutner-Fischerad167412014-04-13 16:37:57 +0200401#define HAVE_USLEEP 1
Dan Fandrich75214cf2011-06-30 02:59:17 +0200402#define HAVE_UNLOCKED_STDIO 1
403#define HAVE_UNLOCKED_LINE_OPS 1
Timo Teras0a5b3102011-06-29 02:19:58 +0200404#define HAVE_GETLINE 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200405#define HAVE_XTABS 1
Dan Fandrichf533ec82011-06-10 05:17:59 +0200406#define HAVE_MNTENT_H 1
407#define HAVE_NET_ETHERNET_H 1
408#define HAVE_SYS_STATFS_H 1
409
Bernhard Reutner-Fischerad167412014-04-13 16:37:57 +0200410#if defined(__UCLIBC__)
411# if UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32)
412# undef HAVE_STRVERSCMP
413# endif
414# if UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
415# ifndef __UCLIBC_SUSV3_LEGACY__
416# undef HAVE_USLEEP
417# endif
418# endif
Denys Vlasenko1e18a012011-06-21 17:12:52 +0200419#endif
420
Dan Fandrich21a542d2009-10-27 11:05:00 +0100421#if defined(__WATCOMC__)
Denys Vlasenko47061b42011-04-17 23:14:19 +0200422# undef HAVE_DPRINTF
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700423# undef HAVE_GETLINE
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100424# undef HAVE_MEMRCHR
425# undef HAVE_MKDTEMP
426# undef HAVE_SETBIT
Dan Fandrichdc506762011-02-12 22:26:57 -0800427# undef HAVE_STPCPY
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100428# undef HAVE_STRCASESTR
429# undef HAVE_STRCHRNUL
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700430# undef HAVE_STRSEP
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100431# undef HAVE_STRSIGNAL
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200432# undef HAVE_STRVERSCMP
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100433# undef HAVE_VASPRINTF
Dan Fandrich75214cf2011-06-30 02:59:17 +0200434# undef HAVE_UNLOCKED_STDIO
435# undef HAVE_UNLOCKED_LINE_OPS
Dan Fandrichf533ec82011-06-10 05:17:59 +0200436# undef HAVE_NET_ETHERNET_H
Dan Fandrich21a542d2009-10-27 11:05:00 +0100437#endif
438
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200439#if defined(__CYGWIN__)
440# undef HAVE_CLEARENV
441# undef HAVE_FDPRINTF
442# undef HAVE_MEMRCHR
443# undef HAVE_PTSNAME_R
444# undef HAVE_STRVERSCMP
445# undef HAVE_UNLOCKED_LINE_OPS
446#endif
447
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700448/* These BSD-derived OSes share many similarities */
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200449#if (defined __digital__ && defined __unix__) \
450 || defined __APPLE__ \
Denys Vlasenko8e0ad262014-01-08 15:10:54 +0100451 || defined __OpenBSD__ || defined __NetBSD__
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200452# undef HAVE_CLEARENV
453# undef HAVE_FDATASYNC
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700454# undef HAVE_GETLINE
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200455# undef HAVE_MNTENT_H
456# undef HAVE_PTSNAME_R
457# undef HAVE_SYS_STATFS_H
458# undef HAVE_SIGHANDLER_T
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200459# undef HAVE_STRVERSCMP
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200460# undef HAVE_XTABS
461# undef HAVE_DPRINTF
Matthias Andree12854372011-08-28 05:04:07 +0200462# undef HAVE_UNLOCKED_STDIO
463# undef HAVE_UNLOCKED_LINE_OPS
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200464#endif
465
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100466#if defined(__dietlibc__)
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700467# undef HAVE_STRCHRNUL
468#endif
469
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100470#if defined(__APPLE__)
471# undef HAVE_STRCHRNUL
472#endif
473
474#if defined(__FreeBSD__)
Denys Vlasenko51046452015-02-02 03:51:47 +0100475/* users say mempcpy is not present in FreeBSD 9.x */
476# undef HAVE_MEMPCPY
Denys Vlasenko8e0ad262014-01-08 15:10:54 +0100477# undef HAVE_CLEARENV
478# undef HAVE_FDATASYNC
479# undef HAVE_MNTENT_H
480# undef HAVE_PTSNAME_R
481# undef HAVE_SYS_STATFS_H
482# undef HAVE_SIGHANDLER_T
483# undef HAVE_STRVERSCMP
484# undef HAVE_XTABS
485# undef HAVE_UNLOCKED_LINE_OPS
486# include <osreldate.h>
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100487# if __FreeBSD_version < 1000029
Denys Vlasenko8e0ad262014-01-08 15:10:54 +0100488# undef HAVE_STRCHRNUL /* FreeBSD added strchrnul() between 1000028 and 1000029 */
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100489# endif
490#endif
491
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700492#if defined(__NetBSD__)
493# define HAVE_GETLINE 1 /* Recent NetBSD versions have getline() */
494#endif
495
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200496#if defined(__digital__) && defined(__unix__)
497# undef HAVE_STPCPY
498#endif
499
Denys Vlasenkoe0894f52011-09-09 18:00:44 +0200500#if defined(ANDROID) || defined(__ANDROID__)
Matt Whitlock778efe32015-05-03 18:59:50 +0200501# if __ANDROID_API__ < 8
Chris Renshaw6df96122015-12-17 16:42:01 +0100502 /* ANDROID < 8 has no [f]dprintf at all */
Matt Whitlock778efe32015-05-03 18:59:50 +0200503# undef HAVE_DPRINTF
Chris Renshaw6df96122015-12-17 16:42:01 +0100504# elif __ANDROID_API__ < 21
505 /* ANDROID < 21 has fdprintf */
Matt Whitlock778efe32015-05-03 18:59:50 +0200506# define dprintf fdprintf
Chris Renshaw6df96122015-12-17 16:42:01 +0100507# else
508 /* ANDROID >= 21 has standard dprintf */
509# endif
Matt Whitlock778efe32015-05-03 18:59:50 +0200510# if __ANDROID_API__ < 21
511# undef HAVE_TTYNAME_R
512# undef HAVE_GETLINE
513# undef HAVE_STPCPY
514# endif
Matt Whitlockf23e3ec2015-05-03 18:57:44 +0200515# undef HAVE_MEMPCPY
Dan Fandrich71d73132011-06-03 20:51:58 +0200516# undef HAVE_STRCHRNUL
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200517# undef HAVE_STRVERSCMP
Dan Fandrich75214cf2011-06-30 02:59:17 +0200518# undef HAVE_UNLOCKED_LINE_OPS
Dan Fandrichf533ec82011-06-10 05:17:59 +0200519# undef HAVE_NET_ETHERNET_H
Dan Fandrich71d73132011-06-03 20:51:58 +0200520#endif
521
Dan Fandrich21a542d2009-10-27 11:05:00 +0100522/*
523 * Now, define prototypes for all the functions defined in platform.c
524 * These must come after all the HAVE_* macros are defined (or not)
525 */
526
Denys Vlasenko47061b42011-04-17 23:14:19 +0200527#ifndef HAVE_DPRINTF
528extern int dprintf(int fd, const char *format, ...);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100529#endif
530
531#ifndef HAVE_MEMRCHR
532extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
533#endif
534
535#ifndef HAVE_MKDTEMP
536extern char *mkdtemp(char *template) FAST_FUNC;
537#endif
538
Matt Whitlockcee59052015-04-25 21:32:48 +0200539#ifndef HAVE_TTYNAME_R
540#define ttyname_r bb_ttyname_r
541extern int ttyname_r(int fd, char *buf, size_t buflen);
542#endif
543
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100544#ifndef HAVE_SETBIT
545# define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
546# define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
547#endif
548
Chris Rees9ad97d52011-01-20 00:51:52 +0100549#ifndef HAVE_SIGHANDLER_T
550typedef void (*sighandler_t)(int);
551#endif
552
Dan Fandrichdc506762011-02-12 22:26:57 -0800553#ifndef HAVE_STPCPY
554extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
555#endif
556
Denys Vlasenko50a6d862015-01-25 22:08:46 +0100557#ifndef HAVE_MEMPCPY
Denys Vlasenko8c05a742015-01-29 16:41:48 +0100558#include <string.h>
Denys Vlasenko51046452015-02-02 03:51:47 +0100559/* In case we are wrong about !HAVE_MEMPCPY, and toolchain _does_ have
560 * mempcpy(), avoid colliding with it:
561 */
562#define mempcpy bb__mempcpy
Denys Vlasenko50a6d862015-01-25 22:08:46 +0100563static ALWAYS_INLINE void *mempcpy(void *dest, const void *src, size_t len)
564{
565 return memcpy(dest, src, len) + len;
566}
567#endif
568
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100569#ifndef HAVE_STRCASESTR
570extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
571#endif
572
Dan Fandrich21a542d2009-10-27 11:05:00 +0100573#ifndef HAVE_STRCHRNUL
574extern char *strchrnul(const char *s, int c) FAST_FUNC;
575#endif
576
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700577#ifndef HAVE_STRSEP
578extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
579#endif
580
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100581#ifndef HAVE_STRSIGNAL
582/* Not exactly the same: instead of "Stopped" it shows "STOP" etc */
583# define strsignal(sig) get_signame(sig)
Dan Fandrich21a542d2009-10-27 11:05:00 +0100584#endif
585
Bernhard Reutner-Fischerad167412014-04-13 16:37:57 +0200586#ifndef HAVE_USLEEP
587extern int usleep(unsigned) FAST_FUNC;
588#endif
589
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100590#ifndef HAVE_VASPRINTF
591extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
Dan Fandrich21a542d2009-10-27 11:05:00 +0100592#endif
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200593
Timo Teras0a5b3102011-06-29 02:19:58 +0200594#ifndef HAVE_GETLINE
Denys Vlasenko14bd16a2011-07-08 08:49:40 +0200595# include <stdio.h> /* for FILE */
596# include <sys/types.h> /* size_t */
Timo Teras0a5b3102011-06-29 02:19:58 +0200597extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
598#endif
599
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000600#endif