3
0

platform.h 14 KB

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