blob: ea0512f365063a0677a994dd54c4639489132ef7 [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
Denys Vlasenkod206b162020-06-23 09:38:53 +020033#if !__GNUC_PREREQ(5,0)
34# define deprecated(msg) deprecated
35#endif
36
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000037#undef inline
Denis Vlasenkoa7189f02006-11-17 20:29:00 +000038#if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000039/* it's a keyword */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020040#elif __GNUC_PREREQ(2,7)
41# define inline __inline__
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000042#else
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020043# define inline
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000044#endif
45
46#ifndef __const
47# define __const const
48#endif
49
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000050#define UNUSED_PARAM __attribute__ ((__unused__))
51#define NORETURN __attribute__ ((__noreturn__))
Denys Vlasenko619d9b52017-07-28 15:28:33 +020052
53#if __GNUC_PREREQ(4,5)
54# define bb_unreachable(altcode) __builtin_unreachable()
55#else
56# define bb_unreachable(altcode) altcode
57#endif
58
Denys Vlasenko53283ad2009-11-02 14:20:34 +010059/* "The malloc attribute is used to tell the compiler that a function
60 * may be treated as if any non-NULL pointer it returns cannot alias
61 * any other pointer valid when the function returns. This will often
62 * improve optimization. Standard functions with this property include
63 * malloc and calloc. realloc-like functions have this property as long
64 * as the old pointer is never referred to (including comparing it
65 * to the new pointer) after the function returns a non-NULL value."
66 */
67#define RETURNS_MALLOC __attribute__ ((malloc))
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000068#define PACKED __attribute__ ((__packed__))
69#define ALIGNED(m) __attribute__ ((__aligned__(m)))
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020070
Denis Vlasenko0f3a5802008-03-20 13:13:09 +000071/* __NO_INLINE__: some gcc's do not honor inlining! :( */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000072#if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
73# define ALWAYS_INLINE __attribute__ ((always_inline)) inline
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000074/* I've seen a toolchain where I needed __noinline__ instead of noinline */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000075# define NOINLINE __attribute__((__noinline__))
76# if !ENABLE_WERROR
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000077# define DEPRECATED __attribute__ ((__deprecated__))
78# define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000079# else
Denys Vlasenko5a49d282009-05-19 13:18:45 +020080# define DEPRECATED
81# define UNUSED_PARAM_RESULT
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000082# endif
Denis Vlasenko98636eb2008-05-09 17:59:34 +000083#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +020084# define ALWAYS_INLINE inline
85# define NOINLINE
86# define DEPRECATED
87# define UNUSED_PARAM_RESULT
Denis Vlasenko98636eb2008-05-09 17:59:34 +000088#endif
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000089
Bartosz Golaszewskibf0f2c72014-06-26 14:31:05 +020090/* used by unit test machinery to run registration functions before calling main() */
91#define INIT_FUNC __attribute__ ((constructor))
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020092
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000093/* -fwhole-program makes all symbols local. The attribute externally_visible
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +020094 * forces a symbol global. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000095#if __GNUC_PREREQ(4,1)
96# define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000097//__attribute__ ((__externally_visible__))
Denis Vlasenko98636eb2008-05-09 17:59:34 +000098#else
99# define EXTERNALLY_VISIBLE
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000100#endif
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000101
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100102/* At 4.4 gcc become much more anal about this, need to use "aliased" types */
103#if __GNUC_PREREQ(4,4)
104# define FIX_ALIASING __attribute__((__may_alias__))
105#else
106# define FIX_ALIASING
107#endif
108
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000109/* We use __extension__ in some places to suppress -pedantic warnings
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200110 * about GCC extensions. This feature didn't work properly before
111 * gcc 2.8. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +0000112#if !__GNUC_PREREQ(2,8)
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000113# ifndef __extension__
114# define __extension__
115# endif
116#endif
117
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000118/* FAST_FUNC is a qualifier which (possibly) makes function call faster
119 * and/or smaller by using modified ABI. It is usually only needed
120 * on non-static, busybox internal functions. Recent versions of gcc
121 * optimize statics automatically. FAST_FUNC on static is required
Kang-Che Sungf10f7a22017-01-05 09:25:03 +0800122 * only if you need to match a function pointer's type.
123 * FAST_FUNC may not work well with -flto so allow user to disable this.
124 * (-DFAST_FUNC= )
125 */
126#ifndef FAST_FUNC
127# if __GNUC_PREREQ(3,0) && defined(i386)
Denis Vlasenkoac2b50e2008-06-27 04:30:48 +0000128/* stdcall makes callee to pop arguments from stack, not caller */
Kang-Che Sungf10f7a22017-01-05 09:25:03 +0800129# define FAST_FUNC __attribute__((regparm(3),stdcall))
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000130/* #elif ... - add your favorite arch today! */
Kang-Che Sungf10f7a22017-01-05 09:25:03 +0800131# else
132# define FAST_FUNC
133# endif
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000134#endif
135
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000136/* Make all declarations hidden (-fvisibility flag only affects definitions) */
137/* (don't include system headers after this until corresponding pop!) */
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200138#if __GNUC_PREREQ(4,1) && !defined(__CYGWIN__)
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000139# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)")
140# define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop")
141#else
142# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
143# define POP_SAVED_FUNCTION_VISIBILITY
144#endif
145
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200146/* gcc-2.95 had no va_copy but only __va_copy. */
147#if !__GNUC_PREREQ(3,0)
148# include <stdarg.h>
149# if !defined va_copy && defined __va_copy
150# define va_copy(d,s) __va_copy((d),(s))
151# endif
152#endif
153
154
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000155/* ---- Endian Detection ------------------------------------ */
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000156
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200157#include <limits.h>
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200158#if defined(__digital__) && defined(__unix__)
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +0000159# include <sex.h>
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200160#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
161 || defined(__APPLE__)
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200162# include <sys/resource.h> /* rlimit */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200163# include <machine/endian.h>
164# define bswap_64 __bswap64
165# define bswap_32 __bswap32
166# define bswap_16 __bswap16
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200167#else
Rob Landley15d20a02006-05-29 05:00:44 +0000168# include <byteswap.h>
169# include <endian.h>
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +0000170#endif
171
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200172#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
Mike Frysingerf8855132006-03-28 02:35:56 +0000173# define BB_BIG_ENDIAN 1
174# define BB_LITTLE_ENDIAN 0
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200175#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
176# define BB_BIG_ENDIAN 0
177# define BB_LITTLE_ENDIAN 1
Waldemar Brodkorb95b83ba2010-08-06 09:17:26 +0200178#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
179# define BB_BIG_ENDIAN 1
180# define BB_LITTLE_ENDIAN 0
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200181#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
Mike Frysingerf8855132006-03-28 02:35:56 +0000182# define BB_BIG_ENDIAN 0
183# define BB_LITTLE_ENDIAN 1
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200184#elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
185# define BB_BIG_ENDIAN 1
186# define BB_LITTLE_ENDIAN 0
187#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
188# define BB_BIG_ENDIAN 0
189# define BB_LITTLE_ENDIAN 1
190#elif defined(__386__)
Waldemar Brodkorb95b83ba2010-08-06 09:17:26 +0200191# define BB_BIG_ENDIAN 0
192# define BB_LITTLE_ENDIAN 1
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200193#else
Dan Fandrich21a542d2009-10-27 11:05:00 +0100194# error "Can't determine endianness"
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000195#endif
196
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200197#if ULONG_MAX > 0xffffffff
Denys Vlasenkod005c9f2021-04-24 11:54:50 +0200198/* inline 64-bit bswap only on 64-bit arches */
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200199# define bb_bswap_64(x) bswap_64(x)
200#endif
201
Denis Vlasenko9b0f6e12008-11-01 13:40:32 +0000202/* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
Rob Landleybba7f082006-05-29 05:51:12 +0000203#if BB_BIG_ENDIAN
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200204# define SWAP_BE16(x) (x)
205# define SWAP_BE32(x) (x)
206# define SWAP_BE64(x) (x)
207# define SWAP_LE16(x) bswap_16(x)
208# define SWAP_LE32(x) bswap_32(x)
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200209# define SWAP_LE64(x) bb_bswap_64(x)
Denys Vlasenkobcccad32010-10-16 20:46:35 +0200210# define IF_BIG_ENDIAN(...) __VA_ARGS__
211# define IF_LITTLE_ENDIAN(...)
Rob Landleybba7f082006-05-29 05:51:12 +0000212#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200213# define SWAP_BE16(x) bswap_16(x)
214# define SWAP_BE32(x) bswap_32(x)
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200215# define SWAP_BE64(x) bb_bswap_64(x)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200216# define SWAP_LE16(x) (x)
217# define SWAP_LE32(x) (x)
218# define SWAP_LE64(x) (x)
Denys Vlasenkobcccad32010-10-16 20:46:35 +0200219# define IF_BIG_ENDIAN(...)
220# define IF_LITTLE_ENDIAN(...) __VA_ARGS__
Rob Landleybba7f082006-05-29 05:51:12 +0000221#endif
222
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200223
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000224/* ---- Unaligned access ------------------------------------ */
225
Denys Vlasenko1f4a9872011-01-22 17:31:35 +0100226#include <stdint.h>
Denys Vlasenkob40da222011-01-21 01:16:09 +0100227typedef int bb__aliased_int FIX_ALIASING;
Lauri Kasanenb8173b62013-01-14 05:20:50 +0100228typedef long bb__aliased_long FIX_ALIASING;
Denys Vlasenkob40da222011-01-21 01:16:09 +0100229typedef uint16_t bb__aliased_uint16_t FIX_ALIASING;
230typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
Denys Vlasenko1f5e81f2013-06-27 01:03:19 +0200231typedef uint64_t bb__aliased_uint64_t FIX_ALIASING;
Denys Vlasenkob40da222011-01-21 01:16:09 +0100232
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000233/* NB: unaligned parameter should be a pointer, aligned one -
234 * a lvalue. This makes it more likely to not swap them by mistake
235 */
Joakim Tjernlund80f42752010-02-11 08:48:15 +0100236#if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
Denys Vlasenkof7f70bf2015-02-02 16:07:07 +0100237# define BB_UNALIGNED_MEMACCESS_OK 1
Lauri Kasanenb8173b62013-01-14 05:20:50 +0100238# define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp))
239# define move_from_unaligned_long(v, longp) ((v) = *(bb__aliased_long*)(longp))
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100240# define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p))
241# define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p))
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100242# define move_from_unaligned64(v, u64p) ((v) = *(bb__aliased_uint64_t*)(u64p))
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100243# define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v))
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100244# define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v))
Denys Vlasenko219c9d42018-11-23 18:48:20 +0100245# define move_to_unaligned64(u64p, v) (*(bb__aliased_uint64_t*)(u64p) = (v))
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000246/* #elif ... - add your favorite arch today! */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000247#else
Denys Vlasenkof7f70bf2015-02-02 16:07:07 +0100248# define BB_UNALIGNED_MEMACCESS_OK 0
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000249/* performs reasonably well (gcc usually inlines memcpy here) */
Denys Vlasenko57be1ee2009-11-26 15:26:31 +0100250# define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
Lauri Kasanenb8173b62013-01-14 05:20:50 +0100251# define move_from_unaligned_long(v, longp) (memcpy(&(v), (longp), sizeof(long)))
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200252# define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
253# define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100254# define move_from_unaligned64(v, u64p) (memcpy(&(v), (u64p), 8))
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100255# define move_to_unaligned16(u16p, v) do { \
256 uint16_t __t = (v); \
Denys Vlasenkoe3e32162013-02-27 15:49:38 +0100257 memcpy((u16p), &__t, 2); \
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100258} while (0)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200259# define move_to_unaligned32(u32p, v) do { \
Denis Vlasenko3be23082009-04-17 22:20:44 +0000260 uint32_t __t = (v); \
261 memcpy((u32p), &__t, 4); \
262} while (0)
Denys Vlasenko219c9d42018-11-23 18:48:20 +0100263# define move_to_unaligned64(u64p, v) do { \
264 uint64_t __t = (v); \
265 memcpy((u64p), &__t, 8); \
266} while (0)
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000267#endif
268
Denys Vlasenko2c1258c2017-07-15 20:22:25 +0200269/* Unaligned, fixed-endian accessors */
270#define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
271#define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
272#define put_unaligned_le32(val, buf) move_to_unaligned32(buf, SWAP_LE32(val))
273#define put_unaligned_be32(val, buf) move_to_unaligned32(buf, SWAP_BE32(val))
274
Denys Vlasenkobac929b2017-08-10 12:40:48 +0200275/* unxz needs an aligned fixed-endian accessor.
276 * (however, the compiler does not realize it's aligned, the cast is still necessary)
277 */
278#define get_le32(u32p) ({ uint32_t v = *(bb__aliased_uint32_t*)(u32p); SWAP_LE32(v); })
279
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000280
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200281/* ---- Size-saving "small" ints (arch-dependent) ----------- */
Bernhard Reutner-Fischerbe862092007-03-19 15:15:06 +0000282
Bernhard Reutner-Fischerc966ba42007-01-18 10:32:09 +0000283#if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
284/* add other arches which benefit from this... */
285typedef signed char smallint;
286typedef unsigned char smalluint;
287#else
288/* for arches where byte accesses generate larger code: */
289typedef int smallint;
290typedef unsigned smalluint;
291#endif
292
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000293/* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
294#if (defined __digital__ && defined __unix__)
295/* old system without (proper) C99 support */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200296# define bool smalluint
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000297#else
298/* modern system, so use it */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200299# include <stdbool.h>
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000300#endif
301
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200302
303/*----- Kernel versioning ------------------------------------*/
304
305#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
306
Mike Frysinger445e7542013-03-12 11:13:22 -0400307#ifdef __UCLIBC__
308# define UCLIBC_VERSION KERNEL_VERSION(__UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__)
309#else
310# define UCLIBC_VERSION 0
311#endif
312
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200313
314/* ---- Miscellaneous --------------------------------------- */
315
316#if defined __GLIBC__ \
317 || defined __UCLIBC__ \
318 || defined __dietlibc__ \
Denys Vlasenko14bd16a2011-07-08 08:49:40 +0200319 || defined __BIONIC__ \
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200320 || defined _NEWLIB_VERSION
321# include <features.h>
322#endif
323
324/* Define bb_setpgrp */
Alex Samorukovd4624d32021-01-04 01:33:32 +0100325#if (defined(__digital__) && defined(__unix__)) || defined(__FreeBSD__)
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200326/* use legacy setpgrp(pid_t, pid_t) for now. move to platform.c */
327# define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
328#else
329# define bb_setpgrp() setpgrp()
330#endif
331
332/* fdprintf is more readable, we used it before dprintf was standardized */
333#include <unistd.h>
334#define fdprintf dprintf
335
336/* Useful for defeating gcc's alignment of "char message[]"-like data */
Denys Vlasenko2ffd7102012-08-06 17:17:15 +0200337#if !defined(__s390__)
338 /* on s390[x], non-word-aligned data accesses require larger code */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200339# define ALIGN1 __attribute__((aligned(1)))
340# define ALIGN2 __attribute__((aligned(2)))
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700341# define ALIGN4 __attribute__((aligned(4)))
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000342#else
343/* Arches which MUST have 2 or 4 byte alignment for everything are here */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200344# define ALIGN1
345# define ALIGN2
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700346# define ALIGN4
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000347#endif
Denys Vlasenko965b7952020-11-30 13:03:03 +0100348#define ALIGN8 __attribute__((aligned(8)))
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100349#define ALIGN_INT __attribute__((aligned(sizeof(int))))
Denys Vlasenko965b7952020-11-30 13:03:03 +0100350#define ALIGN_PTR __attribute__((aligned(sizeof(void*))))
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000351
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200352/*
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000353 * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
354 * For earlier versions there is no reliable way to check if we are building
Denis Vlasenko9e589212008-01-24 01:33:42 +0000355 * for a mmu-less system.
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000356 */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +0000357#if ENABLE_NOMMU || \
Mike Frysinger445e7542013-03-12 11:13:22 -0400358 (defined __UCLIBC__ && \
359 UCLIBC_VERSION > KERNEL_VERSION(0, 9, 28) && \
360 !defined __ARCH_USE_MMU__)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200361# define BB_MMU 0
362# define USE_FOR_NOMMU(...) __VA_ARGS__
363# define USE_FOR_MMU(...)
Denis Vlasenko473dae02007-04-11 07:04:23 +0000364#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200365# define BB_MMU 1
366# define USE_FOR_NOMMU(...)
367# define USE_FOR_MMU(...) __VA_ARGS__
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000368#endif
369
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200370#if defined(__digital__) && defined(__unix__)
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000371# include <standards.h>
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000372# include <inttypes.h>
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000373# define PRIu32 "u"
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000374# if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
375# define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
376# endif
377# if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
378# define ADJ_FREQUENCY MOD_FREQUENCY
379# endif
380# if !defined ADJ_TIMECONST && defined MOD_TIMECONST
381# define ADJ_TIMECONST MOD_TIMECONST
382# endif
383# if !defined ADJ_TICK && defined MOD_CLKB
384# define ADJ_TICK MOD_CLKB
385# endif
Rob Landley18958e92006-06-13 18:28:33 +0000386#endif
387
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200388#if defined(__CYGWIN__)
389# define MAXSYMLINKS SYMLOOP_MAX
390#endif
391
Tias Guns36451952012-06-10 14:26:32 +0200392#if defined(ANDROID) || defined(__ANDROID__)
393# define BB_ADDITIONAL_PATH ":/system/sbin:/system/bin:/system/xbin"
394# define SYS_ioprio_set __NR_ioprio_set
395# define SYS_ioprio_get __NR_ioprio_get
396#endif
397
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200398
399/* ---- Who misses what? ------------------------------------ */
400
Dan Fandrichf533ec82011-06-10 05:17:59 +0200401/* Assume all these functions and header files exist by default.
402 * Platforms where it is not true will #undef them below.
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200403 */
404#define HAVE_CLEARENV 1
405#define HAVE_FDATASYNC 1
406#define HAVE_DPRINTF 1
407#define HAVE_MEMRCHR 1
408#define HAVE_MKDTEMP 1
Matt Whitlockcee59052015-04-25 21:32:48 +0200409#define HAVE_TTYNAME_R 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200410#define HAVE_PTSNAME_R 1
411#define HAVE_SETBIT 1
412#define HAVE_SIGHANDLER_T 1
413#define HAVE_STPCPY 1
Denys Vlasenko33745b12021-02-18 13:44:27 +0100414#define HAVE_STPNCPY 1
Denys Vlasenko50a6d862015-01-25 22:08:46 +0100415#define HAVE_MEMPCPY 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200416#define HAVE_STRCASESTR 1
417#define HAVE_STRCHRNUL 1
418#define HAVE_STRSEP 1
419#define HAVE_STRSIGNAL 1
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200420#define HAVE_STRVERSCMP 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200421#define HAVE_VASPRINTF 1
Bernhard Reutner-Fischerad167412014-04-13 16:37:57 +0200422#define HAVE_USLEEP 1
Dan Fandrich75214cf2011-06-30 02:59:17 +0200423#define HAVE_UNLOCKED_STDIO 1
424#define HAVE_UNLOCKED_LINE_OPS 1
Timo Teras0a5b3102011-06-29 02:19:58 +0200425#define HAVE_GETLINE 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200426#define HAVE_XTABS 1
Dan Fandrichf533ec82011-06-10 05:17:59 +0200427#define HAVE_MNTENT_H 1
428#define HAVE_NET_ETHERNET_H 1
429#define HAVE_SYS_STATFS_H 1
Ron Yorstonbe366e52017-07-27 13:53:39 +0100430#define HAVE_PRINTF_PERCENTM 1
Denys Vlasenkoc0943ac2021-04-14 22:14:36 +0200431#define HAVE_WAIT3 1
Ron Yorstona1b0d382020-07-23 08:32:27 +0100432#define HAVE_DEV_FD 1
433#define DEV_FD_PREFIX "/dev/fd/"
Dan Fandrichf533ec82011-06-10 05:17:59 +0200434
Bernhard Reutner-Fischerad167412014-04-13 16:37:57 +0200435#if defined(__UCLIBC__)
436# if UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32)
437# undef HAVE_STRVERSCMP
438# endif
439# if UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
440# ifndef __UCLIBC_SUSV3_LEGACY__
441# undef HAVE_USLEEP
442# endif
443# endif
Denys Vlasenko1e18a012011-06-21 17:12:52 +0200444#endif
445
Dan Fandrich21a542d2009-10-27 11:05:00 +0100446#if defined(__WATCOMC__)
Denys Vlasenko47061b42011-04-17 23:14:19 +0200447# undef HAVE_DPRINTF
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700448# undef HAVE_GETLINE
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100449# undef HAVE_MEMRCHR
450# undef HAVE_MKDTEMP
451# undef HAVE_SETBIT
Dan Fandrichdc506762011-02-12 22:26:57 -0800452# undef HAVE_STPCPY
Denys Vlasenko33745b12021-02-18 13:44:27 +0100453# undef HAVE_STPNCPY
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100454# undef HAVE_STRCASESTR
455# undef HAVE_STRCHRNUL
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700456# undef HAVE_STRSEP
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100457# undef HAVE_STRSIGNAL
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200458# undef HAVE_STRVERSCMP
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100459# undef HAVE_VASPRINTF
Dan Fandrich75214cf2011-06-30 02:59:17 +0200460# undef HAVE_UNLOCKED_STDIO
461# undef HAVE_UNLOCKED_LINE_OPS
Dan Fandrichf533ec82011-06-10 05:17:59 +0200462# undef HAVE_NET_ETHERNET_H
Dan Fandrich21a542d2009-10-27 11:05:00 +0100463#endif
464
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200465#if defined(__CYGWIN__)
466# undef HAVE_CLEARENV
467# undef HAVE_FDPRINTF
468# undef HAVE_MEMRCHR
469# undef HAVE_PTSNAME_R
470# undef HAVE_STRVERSCMP
471# undef HAVE_UNLOCKED_LINE_OPS
472#endif
473
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700474/* These BSD-derived OSes share many similarities */
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200475#if (defined __digital__ && defined __unix__) \
476 || defined __APPLE__ \
Denys Vlasenko8e0ad262014-01-08 15:10:54 +0100477 || defined __OpenBSD__ || defined __NetBSD__
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200478# undef HAVE_CLEARENV
479# undef HAVE_FDATASYNC
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700480# undef HAVE_GETLINE
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200481# undef HAVE_MNTENT_H
482# undef HAVE_PTSNAME_R
483# undef HAVE_SYS_STATFS_H
484# undef HAVE_SIGHANDLER_T
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200485# undef HAVE_STRVERSCMP
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200486# undef HAVE_XTABS
487# undef HAVE_DPRINTF
Matthias Andree12854372011-08-28 05:04:07 +0200488# undef HAVE_UNLOCKED_STDIO
489# undef HAVE_UNLOCKED_LINE_OPS
Ron Yorstonbe366e52017-07-27 13:53:39 +0100490# undef HAVE_PRINTF_PERCENTM
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200491#endif
492
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100493#if defined(__dietlibc__)
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700494# undef HAVE_STRCHRNUL
495#endif
496
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100497#if defined(__APPLE__)
498# undef HAVE_STRCHRNUL
499#endif
500
501#if defined(__FreeBSD__)
Denys Vlasenko51046452015-02-02 03:51:47 +0100502/* users say mempcpy is not present in FreeBSD 9.x */
503# undef HAVE_MEMPCPY
Denys Vlasenko8e0ad262014-01-08 15:10:54 +0100504# undef HAVE_CLEARENV
505# undef HAVE_FDATASYNC
506# undef HAVE_MNTENT_H
507# undef HAVE_PTSNAME_R
508# undef HAVE_SYS_STATFS_H
509# undef HAVE_SIGHANDLER_T
510# undef HAVE_STRVERSCMP
511# undef HAVE_XTABS
512# undef HAVE_UNLOCKED_LINE_OPS
Ron Yorstonbe366e52017-07-27 13:53:39 +0100513# undef HAVE_PRINTF_PERCENTM
Denys Vlasenko8e0ad262014-01-08 15:10:54 +0100514# include <osreldate.h>
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100515# if __FreeBSD_version < 1000029
Denys Vlasenko8e0ad262014-01-08 15:10:54 +0100516# undef HAVE_STRCHRNUL /* FreeBSD added strchrnul() between 1000028 and 1000029 */
Denys Vlasenko432fbd72014-01-07 14:09:47 +0100517# endif
518#endif
519
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700520#if defined(__NetBSD__)
521# define HAVE_GETLINE 1 /* Recent NetBSD versions have getline() */
522#endif
523
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200524#if defined(__digital__) && defined(__unix__)
525# undef HAVE_STPCPY
Denys Vlasenko33745b12021-02-18 13:44:27 +0100526# undef HAVE_STPNCPY
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200527#endif
528
Denys Vlasenkoe0894f52011-09-09 18:00:44 +0200529#if defined(ANDROID) || defined(__ANDROID__)
Matt Whitlock778efe32015-05-03 18:59:50 +0200530# if __ANDROID_API__ < 8
Chris Renshaw6df96122015-12-17 16:42:01 +0100531 /* ANDROID < 8 has no [f]dprintf at all */
Matt Whitlock778efe32015-05-03 18:59:50 +0200532# undef HAVE_DPRINTF
Chris Renshaw6df96122015-12-17 16:42:01 +0100533# elif __ANDROID_API__ < 21
534 /* ANDROID < 21 has fdprintf */
Matt Whitlock778efe32015-05-03 18:59:50 +0200535# define dprintf fdprintf
Chris Renshaw6df96122015-12-17 16:42:01 +0100536# else
537 /* ANDROID >= 21 has standard dprintf */
538# endif
Matt Whitlock778efe32015-05-03 18:59:50 +0200539# if __ANDROID_API__ < 21
540# undef HAVE_TTYNAME_R
541# undef HAVE_GETLINE
542# undef HAVE_STPCPY
Denys Vlasenko33745b12021-02-18 13:44:27 +0100543# undef HAVE_STPNCPY
Matt Whitlock778efe32015-05-03 18:59:50 +0200544# endif
Denys Vlasenkoc0943ac2021-04-14 22:14:36 +0200545# if __ANDROID_API__ >= 21
546# undef HAVE_WAIT3
547# endif
Matt Whitlockf23e3ec2015-05-03 18:57:44 +0200548# undef HAVE_MEMPCPY
Dan Fandrich71d73132011-06-03 20:51:58 +0200549# undef HAVE_STRCHRNUL
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200550# undef HAVE_STRVERSCMP
Dan Fandrich75214cf2011-06-30 02:59:17 +0200551# undef HAVE_UNLOCKED_LINE_OPS
Dan Fandrichf533ec82011-06-10 05:17:59 +0200552# undef HAVE_NET_ETHERNET_H
Ron Yorstonbe366e52017-07-27 13:53:39 +0100553# undef HAVE_PRINTF_PERCENTM
Dan Fandrich71d73132011-06-03 20:51:58 +0200554#endif
555
Dan Fandrich21a542d2009-10-27 11:05:00 +0100556/*
557 * Now, define prototypes for all the functions defined in platform.c
558 * These must come after all the HAVE_* macros are defined (or not)
559 */
560
Denys Vlasenko47061b42011-04-17 23:14:19 +0200561#ifndef HAVE_DPRINTF
562extern int dprintf(int fd, const char *format, ...);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100563#endif
564
565#ifndef HAVE_MEMRCHR
566extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
567#endif
568
569#ifndef HAVE_MKDTEMP
570extern char *mkdtemp(char *template) FAST_FUNC;
571#endif
572
Matt Whitlockcee59052015-04-25 21:32:48 +0200573#ifndef HAVE_TTYNAME_R
574#define ttyname_r bb_ttyname_r
575extern int ttyname_r(int fd, char *buf, size_t buflen);
576#endif
577
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100578#ifndef HAVE_SETBIT
579# define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
580# define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
581#endif
582
Chris Rees9ad97d52011-01-20 00:51:52 +0100583#ifndef HAVE_SIGHANDLER_T
584typedef void (*sighandler_t)(int);
585#endif
586
Dan Fandrichdc506762011-02-12 22:26:57 -0800587#ifndef HAVE_STPCPY
588extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
589#endif
590
Denys Vlasenko33745b12021-02-18 13:44:27 +0100591#ifndef HAVE_STPNCPY
592extern char *stpncpy(char *p, const char *to_add, size_t n) FAST_FUNC;
593#endif
594
Denys Vlasenko50a6d862015-01-25 22:08:46 +0100595#ifndef HAVE_MEMPCPY
Denys Vlasenko8c05a742015-01-29 16:41:48 +0100596#include <string.h>
Denys Vlasenko51046452015-02-02 03:51:47 +0100597/* In case we are wrong about !HAVE_MEMPCPY, and toolchain _does_ have
598 * mempcpy(), avoid colliding with it:
599 */
600#define mempcpy bb__mempcpy
Denys Vlasenko50a6d862015-01-25 22:08:46 +0100601static ALWAYS_INLINE void *mempcpy(void *dest, const void *src, size_t len)
602{
603 return memcpy(dest, src, len) + len;
604}
605#endif
606
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100607#ifndef HAVE_STRCASESTR
608extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
609#endif
610
Dan Fandrich21a542d2009-10-27 11:05:00 +0100611#ifndef HAVE_STRCHRNUL
612extern char *strchrnul(const char *s, int c) FAST_FUNC;
613#endif
614
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700615#ifndef HAVE_STRSEP
616extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
617#endif
618
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100619#ifndef HAVE_STRSIGNAL
620/* Not exactly the same: instead of "Stopped" it shows "STOP" etc */
621# define strsignal(sig) get_signame(sig)
Dan Fandrich21a542d2009-10-27 11:05:00 +0100622#endif
623
Bernhard Reutner-Fischerad167412014-04-13 16:37:57 +0200624#ifndef HAVE_USLEEP
625extern int usleep(unsigned) FAST_FUNC;
626#endif
627
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100628#ifndef HAVE_VASPRINTF
629extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
Dan Fandrich21a542d2009-10-27 11:05:00 +0100630#endif
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200631
Timo Teras0a5b3102011-06-29 02:19:58 +0200632#ifndef HAVE_GETLINE
Denys Vlasenko14bd16a2011-07-08 08:49:40 +0200633# include <stdio.h> /* for FILE */
634# include <sys/types.h> /* size_t */
Timo Teras0a5b3102011-06-29 02:19:58 +0200635extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
636#endif
637
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000638#endif