1
2
3
4
5
6
7#ifndef BB_PLATFORM_H
8#define BB_PLATFORM_H 1
9
10
11
12#undef __GNUC_PREREQ
13#if defined __GNUC__ && defined __GNUC_MINOR__
14# define __GNUC_PREREQ(maj, min) \
15 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
16#else
17# define __GNUC_PREREQ(maj, min) 0
18#endif
19
20
21#if !__GNUC_PREREQ(2,92)
22# ifndef __restrict
23# define __restrict
24# endif
25#endif
26
27#if !__GNUC_PREREQ(2,7)
28# ifndef __attribute__
29# define __attribute__(x)
30# endif
31#endif
32
33#if !__GNUC_PREREQ(5,0)
34# define deprecated(msg) deprecated
35#endif
36
37#undef inline
38#if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
39
40#elif __GNUC_PREREQ(2,7)
41# define inline __inline__
42#else
43# define inline
44#endif
45
46#ifndef __const
47# define __const const
48#endif
49
50#define UNUSED_PARAM __attribute__ ((__unused__))
51#define NORETURN __attribute__ ((__noreturn__))
52
53#if __GNUC_PREREQ(4,5)
54# define bb_unreachable(altcode) __builtin_unreachable()
55#else
56# define bb_unreachable(altcode) altcode
57#endif
58
59
60
61
62
63
64
65
66
67#define RETURNS_MALLOC __attribute__ ((malloc))
68#define PACKED __attribute__ ((__packed__))
69#define ALIGNED(m) __attribute__ ((__aligned__(m)))
70
71
72#if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
73# define ALWAYS_INLINE __attribute__ ((always_inline)) inline
74
75# define NOINLINE __attribute__((__noinline__))
76# if !ENABLE_WERROR
77# define DEPRECATED __attribute__ ((__deprecated__))
78# define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
79# else
80# define DEPRECATED
81# define UNUSED_PARAM_RESULT
82# endif
83#else
84# define ALWAYS_INLINE inline
85# define NOINLINE
86# define DEPRECATED
87# define UNUSED_PARAM_RESULT
88#endif
89
90
91#define INIT_FUNC __attribute__ ((constructor))
92
93
94
95#if __GNUC_PREREQ(4,1)
96# define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
97
98#else
99# define EXTERNALLY_VISIBLE
100#endif
101
102
103#if __GNUC_PREREQ(4,4)
104# define FIX_ALIASING __attribute__((__may_alias__))
105#else
106# define FIX_ALIASING
107#endif
108
109
110
111
112#if !__GNUC_PREREQ(2,8)
113# ifndef __extension__
114# define __extension__
115# endif
116#endif
117
118
119
120
121
122
123
124
125
126#ifndef FAST_FUNC
127# if __GNUC_PREREQ(3,0) && defined(i386)
128
129# define FAST_FUNC __attribute__((regparm(3),stdcall))
130
131# else
132# define FAST_FUNC
133# endif
134#endif
135
136
137
138#if __GNUC_PREREQ(4,1) && !defined(__CYGWIN__)
139# 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
146
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
155
156
157#include <limits.h>
158#if defined(__digital__) && defined(__unix__)
159# include <sex.h>
160#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
161 || defined(__APPLE__)
162# include <sys/resource.h>
163# include <machine/endian.h>
164# define bswap_64 __bswap64
165# define bswap_32 __bswap32
166# define bswap_16 __bswap16
167#else
168# include <byteswap.h>
169# include <endian.h>
170#endif
171
172#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
173# define BB_BIG_ENDIAN 1
174# define BB_LITTLE_ENDIAN 0
175#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
176# define BB_BIG_ENDIAN 0
177# define BB_LITTLE_ENDIAN 1
178#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
179# define BB_BIG_ENDIAN 1
180# define BB_LITTLE_ENDIAN 0
181#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
182# define BB_BIG_ENDIAN 0
183# define BB_LITTLE_ENDIAN 1
184#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__)
191# define BB_BIG_ENDIAN 0
192# define BB_LITTLE_ENDIAN 1
193#else
194# error "Can't determine endianness"
195#endif
196
197#if ULONG_MAX > 0xffffffff
198
199# define bb_bswap_64(x) bswap_64(x)
200#endif
201
202
203#if BB_BIG_ENDIAN
204# 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)
209# define SWAP_LE64(x) bb_bswap_64(x)
210# define IF_BIG_ENDIAN(...) __VA_ARGS__
211# define IF_LITTLE_ENDIAN(...)
212#else
213# define SWAP_BE16(x) bswap_16(x)
214# define SWAP_BE32(x) bswap_32(x)
215# define SWAP_BE64(x) bb_bswap_64(x)
216# define SWAP_LE16(x) (x)
217# define SWAP_LE32(x) (x)
218# define SWAP_LE64(x) (x)
219# define IF_BIG_ENDIAN(...)
220# define IF_LITTLE_ENDIAN(...) __VA_ARGS__
221#endif
222
223
224
225
226#include <stdint.h>
227typedef int bb__aliased_int FIX_ALIASING;
228typedef long bb__aliased_long FIX_ALIASING;
229typedef uint16_t bb__aliased_uint16_t FIX_ALIASING;
230typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
231typedef uint64_t bb__aliased_uint64_t FIX_ALIASING;
232
233
234
235
236#if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
237# define BB_UNALIGNED_MEMACCESS_OK 1
238# 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))
240# 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))
242# define move_from_unaligned64(v, u64p) ((v) = *(bb__aliased_uint64_t*)(u64p))
243# define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v))
244# define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v))
245# define move_to_unaligned64(u64p, v) (*(bb__aliased_uint64_t*)(u64p) = (v))
246
247#else
248# define BB_UNALIGNED_MEMACCESS_OK 0
249
250# define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
251# define move_from_unaligned_long(v, longp) (memcpy(&(v), (longp), sizeof(long)))
252# define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
253# define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
254# define move_from_unaligned64(v, u64p) (memcpy(&(v), (u64p), 8))
255# define move_to_unaligned16(u16p, v) do { \
256 uint16_t __t = (v); \
257 memcpy((u16p), &__t, 2); \
258} while (0)
259# define move_to_unaligned32(u32p, v) do { \
260 uint32_t __t = (v); \
261 memcpy((u32p), &__t, 4); \
262} while (0)
263# define move_to_unaligned64(u64p, v) do { \
264 uint64_t __t = (v); \
265 memcpy((u64p), &__t, 8); \
266} while (0)
267#endif
268
269
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
275
276
277
278#define get_le32(u32p) ({ uint32_t v = *(bb__aliased_uint32_t*)(u32p); SWAP_LE32(v); })
279
280
281
282
283#if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
284
285typedef signed char smallint;
286typedef unsigned char smalluint;
287#else
288
289typedef int smallint;
290typedef unsigned smalluint;
291#endif
292
293
294#if (defined __digital__ && defined __unix__)
295
296# define bool smalluint
297#else
298
299# include <stdbool.h>
300#endif
301
302
303
304
305#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
306
307#ifdef __UCLIBC__
308# define UCLIBC_VERSION KERNEL_VERSION(__UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__)
309#else
310# define UCLIBC_VERSION 0
311#endif
312
313
314
315
316#if defined __GLIBC__ \
317 || defined __UCLIBC__ \
318 || defined __dietlibc__ \
319 || defined __BIONIC__ \
320 || defined _NEWLIB_VERSION
321# include <features.h>
322#endif
323
324
325#if (defined(__digital__) && defined(__unix__)) || defined(__FreeBSD__)
326
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
333#include <unistd.h>
334#define fdprintf dprintf
335
336
337#if !defined(__s390__)
338
339# define ALIGN1 __attribute__((aligned(1)))
340# define ALIGN2 __attribute__((aligned(2)))
341# define ALIGN4 __attribute__((aligned(4)))
342#else
343
344# define ALIGN1
345# define ALIGN2
346# define ALIGN4
347#endif
348#define ALIGN8 __attribute__((aligned(8)))
349#define ALIGN_PTR __attribute__((aligned(sizeof(void*))))
350
351
352
353
354
355
356#if ENABLE_NOMMU || \
357 (defined __UCLIBC__ && \
358 UCLIBC_VERSION > KERNEL_VERSION(0, 9, 28) && \
359 !defined __ARCH_USE_MMU__)
360# define BB_MMU 0
361# define USE_FOR_NOMMU(...) __VA_ARGS__
362# define USE_FOR_MMU(...)
363#else
364# define BB_MMU 1
365# define USE_FOR_NOMMU(...)
366# define USE_FOR_MMU(...) __VA_ARGS__
367#endif
368
369#if defined(__digital__) && defined(__unix__)
370# include <standards.h>
371# include <inttypes.h>
372# define PRIu32 "u"
373# if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
374# define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
375# endif
376# if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
377# define ADJ_FREQUENCY MOD_FREQUENCY
378# endif
379# if !defined ADJ_TIMECONST && defined MOD_TIMECONST
380# define ADJ_TIMECONST MOD_TIMECONST
381# endif
382# if !defined ADJ_TICK && defined MOD_CLKB
383# define ADJ_TICK MOD_CLKB
384# endif
385#endif
386
387#if defined(__CYGWIN__)
388# define MAXSYMLINKS SYMLOOP_MAX
389#endif
390
391#if defined(ANDROID) || defined(__ANDROID__)
392# define BB_ADDITIONAL_PATH ":/system/sbin:/system/bin:/system/xbin"
393# define SYS_ioprio_set __NR_ioprio_set
394# define SYS_ioprio_get __NR_ioprio_get
395#endif
396
397
398
399
400
401
402
403#define HAVE_CLEARENV 1
404#define HAVE_FDATASYNC 1
405#define HAVE_DPRINTF 1
406#define HAVE_MEMRCHR 1
407#define HAVE_MKDTEMP 1
408#define HAVE_TTYNAME_R 1
409#define HAVE_PTSNAME_R 1
410#define HAVE_SETBIT 1
411#define HAVE_SIGHANDLER_T 1
412#define HAVE_STPCPY 1
413#define HAVE_STPNCPY 1
414#define HAVE_MEMPCPY 1
415#define HAVE_STRCASESTR 1
416#define HAVE_STRCHRNUL 1
417#define HAVE_STRSEP 1
418#define HAVE_STRSIGNAL 1
419#define HAVE_STRVERSCMP 1
420#define HAVE_VASPRINTF 1
421#define HAVE_USLEEP 1
422#define HAVE_UNLOCKED_STDIO 1
423#define HAVE_UNLOCKED_LINE_OPS 1
424#define HAVE_GETLINE 1
425#define HAVE_XTABS 1
426#define HAVE_MNTENT_H 1
427#define HAVE_NET_ETHERNET_H 1
428#define HAVE_SYS_STATFS_H 1
429#define HAVE_PRINTF_PERCENTM 1
430#define HAVE_WAIT3 1
431#define HAVE_DEV_FD 1
432#define DEV_FD_PREFIX "/dev/fd/"
433
434#if defined(__UCLIBC__)
435# if UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32)
436# undef HAVE_STRVERSCMP
437# endif
438# if UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
439# ifndef __UCLIBC_SUSV3_LEGACY__
440# undef HAVE_USLEEP
441# endif
442# endif
443#endif
444
445#if defined(__WATCOMC__)
446# undef HAVE_DPRINTF
447# undef HAVE_GETLINE
448# undef HAVE_MEMRCHR
449# undef HAVE_MKDTEMP
450# undef HAVE_SETBIT
451# undef HAVE_STPCPY
452# undef HAVE_STPNCPY
453# undef HAVE_STRCASESTR
454# undef HAVE_STRCHRNUL
455# undef HAVE_STRSEP
456# undef HAVE_STRSIGNAL
457# undef HAVE_STRVERSCMP
458# undef HAVE_VASPRINTF
459# undef HAVE_UNLOCKED_STDIO
460# undef HAVE_UNLOCKED_LINE_OPS
461# undef HAVE_NET_ETHERNET_H
462#endif
463
464#if defined(__CYGWIN__)
465# undef HAVE_CLEARENV
466# undef HAVE_FDPRINTF
467# undef HAVE_MEMRCHR
468# undef HAVE_PTSNAME_R
469# undef HAVE_STRVERSCMP
470# undef HAVE_UNLOCKED_LINE_OPS
471#endif
472
473
474#if (defined __digital__ && defined __unix__) \
475 || defined __APPLE__ \
476 || defined __OpenBSD__ || defined __NetBSD__
477# undef HAVE_CLEARENV
478# undef HAVE_FDATASYNC
479# undef HAVE_GETLINE
480# undef HAVE_MNTENT_H
481# undef HAVE_PTSNAME_R
482# undef HAVE_SYS_STATFS_H
483# undef HAVE_SIGHANDLER_T
484# undef HAVE_STRVERSCMP
485# undef HAVE_XTABS
486# undef HAVE_DPRINTF
487# undef HAVE_UNLOCKED_STDIO
488# undef HAVE_UNLOCKED_LINE_OPS
489# undef HAVE_PRINTF_PERCENTM
490#endif
491
492#if defined(__dietlibc__)
493# undef HAVE_STRCHRNUL
494#endif
495
496#if defined(__APPLE__)
497# undef HAVE_STRCHRNUL
498#endif
499
500#if defined(__FreeBSD__)
501
502# undef HAVE_MEMPCPY
503# undef HAVE_CLEARENV
504# undef HAVE_FDATASYNC
505# undef HAVE_MNTENT_H
506# undef HAVE_PTSNAME_R
507# undef HAVE_SYS_STATFS_H
508# undef HAVE_SIGHANDLER_T
509# undef HAVE_STRVERSCMP
510# undef HAVE_XTABS
511# undef HAVE_UNLOCKED_LINE_OPS
512# undef HAVE_PRINTF_PERCENTM
513# include <osreldate.h>
514# if __FreeBSD_version < 1000029
515# undef HAVE_STRCHRNUL
516# endif
517#endif
518
519#if defined(__NetBSD__)
520# define HAVE_GETLINE 1
521#endif
522
523#if defined(__digital__) && defined(__unix__)
524# undef HAVE_STPCPY
525# undef HAVE_STPNCPY
526#endif
527
528#if defined(ANDROID) || defined(__ANDROID__)
529# if __ANDROID_API__ < 8
530
531# undef HAVE_DPRINTF
532# elif __ANDROID_API__ < 21
533
534# define dprintf fdprintf
535# else
536
537# endif
538# if __ANDROID_API__ < 21
539# undef HAVE_TTYNAME_R
540# undef HAVE_GETLINE
541# undef HAVE_STPCPY
542# undef HAVE_STPNCPY
543# endif
544# if __ANDROID_API__ >= 21
545# undef HAVE_WAIT3
546# endif
547# undef HAVE_MEMPCPY
548# undef HAVE_STRCHRNUL
549# undef HAVE_STRVERSCMP
550# undef HAVE_UNLOCKED_LINE_OPS
551# undef HAVE_NET_ETHERNET_H
552# undef HAVE_PRINTF_PERCENTM
553#endif
554
555
556
557
558
559
560#ifndef HAVE_DPRINTF
561extern int dprintf(int fd, const char *format, ...);
562#endif
563
564#ifndef HAVE_MEMRCHR
565extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
566#endif
567
568#ifndef HAVE_MKDTEMP
569extern char *mkdtemp(char *template) FAST_FUNC;
570#endif
571
572#ifndef HAVE_TTYNAME_R
573#define ttyname_r bb_ttyname_r
574extern int ttyname_r(int fd, char *buf, size_t buflen);
575#endif
576
577#ifndef HAVE_SETBIT
578# define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
579# define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
580#endif
581
582#ifndef HAVE_SIGHANDLER_T
583typedef void (*sighandler_t)(int);
584#endif
585
586#ifndef HAVE_STPCPY
587extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
588#endif
589
590#ifndef HAVE_STPNCPY
591extern char *stpncpy(char *p, const char *to_add, size_t n) FAST_FUNC;
592#endif
593
594#ifndef HAVE_MEMPCPY
595#include <string.h>
596
597
598
599#define mempcpy bb__mempcpy
600static ALWAYS_INLINE void *mempcpy(void *dest, const void *src, size_t len)
601{
602 return memcpy(dest, src, len) + len;
603}
604#endif
605
606#ifndef HAVE_STRCASESTR
607extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
608#endif
609
610#ifndef HAVE_STRCHRNUL
611extern char *strchrnul(const char *s, int c) FAST_FUNC;
612#endif
613
614#ifndef HAVE_STRSEP
615extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
616#endif
617
618#ifndef HAVE_STRSIGNAL
619
620# define strsignal(sig) get_signame(sig)
621#endif
622
623#ifndef HAVE_USLEEP
624extern int usleep(unsigned) FAST_FUNC;
625#endif
626
627#ifndef HAVE_VASPRINTF
628extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
629#endif
630
631#ifndef HAVE_GETLINE
632# include <stdio.h>
633# include <sys/types.h>
634extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
635#endif
636
637#endif
638