3
0

platform.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. /* NB: unaligned parameter should be a pointer, aligned one -
  189. * a lvalue. This makes it more likely to not swap them by mistake
  190. */
  191. #if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
  192. # define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp))
  193. # define move_from_unaligned_long(v, longp) ((v) = *(bb__aliased_long*)(longp))
  194. # define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p))
  195. # define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p))
  196. # define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v))
  197. # define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v))
  198. /* #elif ... - add your favorite arch today! */
  199. #else
  200. /* performs reasonably well (gcc usually inlines memcpy here) */
  201. # define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
  202. # define move_from_unaligned_long(v, longp) (memcpy(&(v), (longp), sizeof(long)))
  203. # define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
  204. # define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
  205. # define move_to_unaligned16(u16p, v) do { \
  206. uint16_t __t = (v); \
  207. memcpy((u16p), &__t, 2); \
  208. } while (0)
  209. # define move_to_unaligned32(u32p, v) do { \
  210. uint32_t __t = (v); \
  211. memcpy((u32p), &__t, 4); \
  212. } while (0)
  213. #endif
  214. /* ---- Size-saving "small" ints (arch-dependent) ----------- */
  215. #if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
  216. /* add other arches which benefit from this... */
  217. typedef signed char smallint;
  218. typedef unsigned char smalluint;
  219. #else
  220. /* for arches where byte accesses generate larger code: */
  221. typedef int smallint;
  222. typedef unsigned smalluint;
  223. #endif
  224. /* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
  225. #if (defined __digital__ && defined __unix__)
  226. /* old system without (proper) C99 support */
  227. # define bool smalluint
  228. #else
  229. /* modern system, so use it */
  230. # include <stdbool.h>
  231. #endif
  232. /*----- Kernel versioning ------------------------------------*/
  233. #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
  234. /* ---- Miscellaneous --------------------------------------- */
  235. #if defined __GLIBC__ \
  236. || defined __UCLIBC__ \
  237. || defined __dietlibc__ \
  238. || defined __BIONIC__ \
  239. || defined _NEWLIB_VERSION
  240. # include <features.h>
  241. #endif
  242. /* Define bb_setpgrp */
  243. #if defined(__digital__) && defined(__unix__)
  244. /* use legacy setpgrp(pid_t, pid_t) for now. move to platform.c */
  245. # define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
  246. #else
  247. # define bb_setpgrp() setpgrp()
  248. #endif
  249. /* fdprintf is more readable, we used it before dprintf was standardized */
  250. #include <unistd.h>
  251. #define fdprintf dprintf
  252. /* Useful for defeating gcc's alignment of "char message[]"-like data */
  253. #if !defined(__s390__)
  254. /* on s390[x], non-word-aligned data accesses require larger code */
  255. # define ALIGN1 __attribute__((aligned(1)))
  256. # define ALIGN2 __attribute__((aligned(2)))
  257. # define ALIGN4 __attribute__((aligned(4)))
  258. #else
  259. /* Arches which MUST have 2 or 4 byte alignment for everything are here */
  260. # define ALIGN1
  261. # define ALIGN2
  262. # define ALIGN4
  263. #endif
  264. /*
  265. * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
  266. * For earlier versions there is no reliable way to check if we are building
  267. * for a mmu-less system.
  268. */
  269. #if ENABLE_NOMMU || \
  270. (defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \
  271. __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__)
  272. # define BB_MMU 0
  273. # define USE_FOR_NOMMU(...) __VA_ARGS__
  274. # define USE_FOR_MMU(...)
  275. #else
  276. # define BB_MMU 1
  277. # define USE_FOR_NOMMU(...)
  278. # define USE_FOR_MMU(...) __VA_ARGS__
  279. #endif
  280. #if defined(__digital__) && defined(__unix__)
  281. # include <standards.h>
  282. # include <inttypes.h>
  283. # define PRIu32 "u"
  284. # if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
  285. # define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
  286. # endif
  287. # if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
  288. # define ADJ_FREQUENCY MOD_FREQUENCY
  289. # endif
  290. # if !defined ADJ_TIMECONST && defined MOD_TIMECONST
  291. # define ADJ_TIMECONST MOD_TIMECONST
  292. # endif
  293. # if !defined ADJ_TICK && defined MOD_CLKB
  294. # define ADJ_TICK MOD_CLKB
  295. # endif
  296. #endif
  297. #if defined(__CYGWIN__)
  298. # define MAXSYMLINKS SYMLOOP_MAX
  299. #endif
  300. #if defined(ANDROID) || defined(__ANDROID__)
  301. # define BB_ADDITIONAL_PATH ":/system/sbin:/system/bin:/system/xbin"
  302. # define SYS_ioprio_set __NR_ioprio_set
  303. # define SYS_ioprio_get __NR_ioprio_get
  304. #endif
  305. /* ---- Who misses what? ------------------------------------ */
  306. /* Assume all these functions and header files exist by default.
  307. * Platforms where it is not true will #undef them below.
  308. */
  309. #define HAVE_CLEARENV 1
  310. #define HAVE_FDATASYNC 1
  311. #define HAVE_DPRINTF 1
  312. #define HAVE_MEMRCHR 1
  313. #define HAVE_MKDTEMP 1
  314. #define HAVE_PTSNAME_R 1
  315. #define HAVE_SETBIT 1
  316. #define HAVE_SIGHANDLER_T 1
  317. #define HAVE_STPCPY 1
  318. #define HAVE_STRCASESTR 1
  319. #define HAVE_STRCHRNUL 1
  320. #define HAVE_STRSEP 1
  321. #define HAVE_STRSIGNAL 1
  322. #define HAVE_STRVERSCMP 1
  323. #define HAVE_VASPRINTF 1
  324. #define HAVE_UNLOCKED_STDIO 1
  325. #define HAVE_UNLOCKED_LINE_OPS 1
  326. #define HAVE_GETLINE 1
  327. #define HAVE_XTABS 1
  328. #define HAVE_MNTENT_H 1
  329. #define HAVE_NET_ETHERNET_H 1
  330. #define HAVE_SYS_STATFS_H 1
  331. #if defined(__UCLIBC_MAJOR__)
  332. # if __UCLIBC_MAJOR__ == 0 \
  333. && ( __UCLIBC_MINOR__ < 9 \
  334. || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 32) \
  335. )
  336. # undef HAVE_STRVERSCMP
  337. # endif
  338. #endif
  339. #if defined(__dietlibc__)
  340. # undef HAVE_STRCHRNUL
  341. #endif
  342. #if defined(__WATCOMC__)
  343. # undef HAVE_DPRINTF
  344. # undef HAVE_GETLINE
  345. # undef HAVE_MEMRCHR
  346. # undef HAVE_MKDTEMP
  347. # undef HAVE_SETBIT
  348. # undef HAVE_STPCPY
  349. # undef HAVE_STRCASESTR
  350. # undef HAVE_STRCHRNUL
  351. # undef HAVE_STRSEP
  352. # undef HAVE_STRSIGNAL
  353. # undef HAVE_STRVERSCMP
  354. # undef HAVE_VASPRINTF
  355. # undef HAVE_UNLOCKED_STDIO
  356. # undef HAVE_UNLOCKED_LINE_OPS
  357. # undef HAVE_NET_ETHERNET_H
  358. #endif
  359. #if defined(__CYGWIN__)
  360. # undef HAVE_CLEARENV
  361. # undef HAVE_FDPRINTF
  362. # undef HAVE_MEMRCHR
  363. # undef HAVE_PTSNAME_R
  364. # undef HAVE_STRVERSCMP
  365. # undef HAVE_UNLOCKED_LINE_OPS
  366. #endif
  367. /* These BSD-derived OSes share many similarities */
  368. #if (defined __digital__ && defined __unix__) \
  369. || defined __APPLE__ \
  370. || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
  371. # undef HAVE_CLEARENV
  372. # undef HAVE_FDATASYNC
  373. # undef HAVE_GETLINE
  374. # undef HAVE_MNTENT_H
  375. # undef HAVE_PTSNAME_R
  376. # undef HAVE_SYS_STATFS_H
  377. # undef HAVE_SIGHANDLER_T
  378. # undef HAVE_STRVERSCMP
  379. # undef HAVE_XTABS
  380. # undef HAVE_DPRINTF
  381. # undef HAVE_UNLOCKED_STDIO
  382. # undef HAVE_UNLOCKED_LINE_OPS
  383. #endif
  384. #if defined(__FreeBSD__)
  385. # undef HAVE_STRCHRNUL
  386. #endif
  387. #if defined(__NetBSD__)
  388. # define HAVE_GETLINE 1 /* Recent NetBSD versions have getline() */
  389. #endif
  390. #if defined(__digital__) && defined(__unix__)
  391. # undef HAVE_STPCPY
  392. #endif
  393. #if defined(ANDROID) || defined(__ANDROID__)
  394. # undef HAVE_DPRINTF
  395. # undef HAVE_GETLINE
  396. # undef HAVE_STPCPY
  397. # undef HAVE_STRCHRNUL
  398. # undef HAVE_STRVERSCMP
  399. # undef HAVE_UNLOCKED_LINE_OPS
  400. # undef HAVE_NET_ETHERNET_H
  401. #endif
  402. /*
  403. * Now, define prototypes for all the functions defined in platform.c
  404. * These must come after all the HAVE_* macros are defined (or not)
  405. */
  406. #ifndef HAVE_DPRINTF
  407. extern int dprintf(int fd, const char *format, ...);
  408. #endif
  409. #ifndef HAVE_MEMRCHR
  410. extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
  411. #endif
  412. #ifndef HAVE_MKDTEMP
  413. extern char *mkdtemp(char *template) FAST_FUNC;
  414. #endif
  415. #ifndef HAVE_SETBIT
  416. # define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
  417. # define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
  418. #endif
  419. #ifndef HAVE_SIGHANDLER_T
  420. typedef void (*sighandler_t)(int);
  421. #endif
  422. #ifndef HAVE_STPCPY
  423. extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
  424. #endif
  425. #ifndef HAVE_STRCASESTR
  426. extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
  427. #endif
  428. #ifndef HAVE_STRCHRNUL
  429. extern char *strchrnul(const char *s, int c) FAST_FUNC;
  430. #endif
  431. #ifndef HAVE_STRSEP
  432. extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
  433. #endif
  434. #ifndef HAVE_STRSIGNAL
  435. /* Not exactly the same: instead of "Stopped" it shows "STOP" etc */
  436. # define strsignal(sig) get_signame(sig)
  437. #endif
  438. #ifndef HAVE_VASPRINTF
  439. extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
  440. #endif
  441. #ifndef HAVE_GETLINE
  442. # include <stdio.h> /* for FILE */
  443. # include <sys/types.h> /* size_t */
  444. extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
  445. #endif
  446. #endif