platform.h 15 KB

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