platform.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. Copyright 2006, Bernhard Reutner-Fischer
  4. Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  5. */
  6. #ifndef BB_PLATFORM_H
  7. #define BB_PLATFORM_H 1
  8. /* Convenience macros to test the version of gcc. */
  9. #undef __GNUC_PREREQ
  10. #if defined __GNUC__ && defined __GNUC_MINOR__
  11. # define __GNUC_PREREQ(maj, min) \
  12. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  13. #else
  14. # define __GNUC_PREREQ(maj, min) 0
  15. #endif
  16. /* __restrict is known in EGCS 1.2 and above. */
  17. #if !__GNUC_PREREQ(2,92)
  18. # ifndef __restrict
  19. # define __restrict
  20. # endif
  21. #endif
  22. /* Define macros for some gcc attributes. This permits us to use the
  23. macros freely, and know that they will come into play for the
  24. version of gcc in which they are supported. */
  25. #if !__GNUC_PREREQ(2,7)
  26. # ifndef __attribute__
  27. # define __attribute__(x)
  28. # endif
  29. #endif
  30. #undef inline
  31. #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
  32. /* it's a keyword */
  33. #elif __GNUC_PREREQ(2,7)
  34. # define inline __inline__
  35. #else
  36. # define inline
  37. #endif
  38. #ifndef __const
  39. # define __const const
  40. #endif
  41. #define UNUSED_PARAM __attribute__ ((__unused__))
  42. #define NORETURN __attribute__ ((__noreturn__))
  43. #define PACKED __attribute__ ((__packed__))
  44. #define ALIGNED(m) __attribute__ ((__aligned__(m)))
  45. /* __NO_INLINE__: some gcc's do not honor inlining! :( */
  46. #if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
  47. # define ALWAYS_INLINE __attribute__ ((always_inline)) inline
  48. /* I've seen a toolchain where I needed __noinline__ instead of noinline */
  49. # define NOINLINE __attribute__((__noinline__))
  50. # if !ENABLE_WERROR
  51. # define DEPRECATED __attribute__ ((__deprecated__))
  52. # define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
  53. # else
  54. # define DEPRECATED
  55. # define UNUSED_PARAM_RESULT
  56. # endif
  57. #else
  58. # define ALWAYS_INLINE inline
  59. # define NOINLINE
  60. # define DEPRECATED
  61. # define UNUSED_PARAM_RESULT
  62. #endif
  63. /* -fwhole-program makes all symbols local. The attribute externally_visible
  64. forces a symbol global. */
  65. #if __GNUC_PREREQ(4,1)
  66. # define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
  67. //__attribute__ ((__externally_visible__))
  68. #else
  69. # define EXTERNALLY_VISIBLE
  70. #endif
  71. /* We use __extension__ in some places to suppress -pedantic warnings
  72. about GCC extensions. This feature didn't work properly before
  73. gcc 2.8. */
  74. #if !__GNUC_PREREQ(2,8)
  75. # ifndef __extension__
  76. # define __extension__
  77. # endif
  78. #endif
  79. /* gcc-2.95 had no va_copy but only __va_copy. */
  80. #if !__GNUC_PREREQ(3,0)
  81. # include <stdarg.h>
  82. # if !defined va_copy && defined __va_copy
  83. # define va_copy(d,s) __va_copy((d),(s))
  84. # endif
  85. #endif
  86. /* FAST_FUNC is a qualifier which (possibly) makes function call faster
  87. * and/or smaller by using modified ABI. It is usually only needed
  88. * on non-static, busybox internal functions. Recent versions of gcc
  89. * optimize statics automatically. FAST_FUNC on static is required
  90. * only if you need to match a function pointer's type */
  91. #if __GNUC_PREREQ(3,0) && defined(i386) /* || defined(__x86_64__)? */
  92. /* stdcall makes callee to pop arguments from stack, not caller */
  93. # define FAST_FUNC __attribute__((regparm(3),stdcall))
  94. /* #elif ... - add your favorite arch today! */
  95. #else
  96. # define FAST_FUNC
  97. #endif
  98. /* Make all declarations hidden (-fvisibility flag only affects definitions) */
  99. /* (don't include system headers after this until corresponding pop!) */
  100. #if __GNUC_PREREQ(4,1)
  101. # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)")
  102. # define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop")
  103. #else
  104. # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
  105. # define POP_SAVED_FUNCTION_VISIBILITY
  106. #endif
  107. /* ---- Endian Detection ------------------------------------ */
  108. #if defined(__digital__) && defined(__unix__)
  109. # include <sex.h>
  110. # define __BIG_ENDIAN__ (BYTE_ORDER == BIG_ENDIAN)
  111. # define __BYTE_ORDER BYTE_ORDER
  112. #elif defined __FreeBSD__
  113. char *strchrnul(const char *s, int c);
  114. # include <sys/resource.h> /* rlimit */
  115. # include <machine/endian.h>
  116. # define bswap_64 __bswap64
  117. # define bswap_32 __bswap32
  118. # define bswap_16 __bswap16
  119. # define __BIG_ENDIAN__ (_BYTE_ORDER == _BIG_ENDIAN)
  120. #elif !defined __APPLE__
  121. # include <byteswap.h>
  122. # include <endian.h>
  123. #endif
  124. #if defined(__BIG_ENDIAN__) && __BIG_ENDIAN__
  125. # define BB_BIG_ENDIAN 1
  126. # define BB_LITTLE_ENDIAN 0
  127. #elif __BYTE_ORDER == __BIG_ENDIAN
  128. # define BB_BIG_ENDIAN 1
  129. # define BB_LITTLE_ENDIAN 0
  130. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  131. # define BB_BIG_ENDIAN 0
  132. # define BB_LITTLE_ENDIAN 1
  133. #else
  134. # error "Can't determine endiannes"
  135. #endif
  136. /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
  137. #if BB_BIG_ENDIAN
  138. # define SWAP_BE16(x) (x)
  139. # define SWAP_BE32(x) (x)
  140. # define SWAP_BE64(x) (x)
  141. # define SWAP_LE16(x) bswap_16(x)
  142. # define SWAP_LE32(x) bswap_32(x)
  143. # define SWAP_LE64(x) bswap_64(x)
  144. #else
  145. # define SWAP_BE16(x) bswap_16(x)
  146. # define SWAP_BE32(x) bswap_32(x)
  147. # define SWAP_BE64(x) bswap_64(x)
  148. # define SWAP_LE16(x) (x)
  149. # define SWAP_LE32(x) (x)
  150. # define SWAP_LE64(x) (x)
  151. #endif
  152. /* ---- Unaligned access ------------------------------------ */
  153. /* NB: unaligned parameter should be a pointer, aligned one -
  154. * a lvalue. This makes it more likely to not swap them by mistake
  155. */
  156. #if defined(i386) || defined(__x86_64__)
  157. # define move_from_unaligned16(v, u16p) ((v) = *(uint16_t*)(u16p))
  158. # define move_from_unaligned32(v, u32p) ((v) = *(uint32_t*)(u32p))
  159. # define move_to_unaligned32(u32p, v) (*(uint32_t*)(u32p) = (v))
  160. /* #elif ... - add your favorite arch today! */
  161. #else
  162. /* performs reasonably well (gcc usually inlines memcpy here) */
  163. # define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
  164. # define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
  165. # define move_to_unaligned32(u32p, v) do { \
  166. uint32_t __t = (v); \
  167. memcpy((u32p), &__t, 4); \
  168. } while (0)
  169. #endif
  170. /* ---- Networking ------------------------------------------ */
  171. #ifndef __APPLE__
  172. # include <arpa/inet.h>
  173. # if !defined(__socklen_t_defined) && !defined(_SOCKLEN_T_DECLARED)
  174. typedef int socklen_t;
  175. # endif
  176. #else
  177. # include <netinet/in.h>
  178. #endif
  179. /* ---- Compiler dependent settings ------------------------- */
  180. #if (defined __digital__ && defined __unix__) \
  181. || defined __APPLE__ || defined __FreeBSD__
  182. # undef HAVE_MNTENT_H
  183. # undef HAVE_SYS_STATFS_H
  184. #else
  185. # define HAVE_MNTENT_H 1
  186. # define HAVE_SYS_STATFS_H 1
  187. #endif
  188. /*----- Kernel versioning ------------------------------------*/
  189. #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
  190. /* ---- Miscellaneous --------------------------------------- */
  191. #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ < 5 && \
  192. !defined(__dietlibc__) && \
  193. !defined(_NEWLIB_VERSION) && \
  194. !(defined __digital__ && defined __unix__)
  195. # error "Sorry, this libc version is not supported :("
  196. #endif
  197. /* Don't perpetuate e2fsck crap into the headers. Clean up e2fsck instead. */
  198. #if defined __GLIBC__ || defined __UCLIBC__ \
  199. || defined __dietlibc__ || defined _NEWLIB_VERSION
  200. # include <features.h>
  201. # define HAVE_FEATURES_H
  202. # include <stdint.h>
  203. # define HAVE_STDINT_H
  204. #elif !defined __APPLE__
  205. /* Largest integral types. */
  206. # if BB_BIG_ENDIAN
  207. /* Looks BROKEN! */
  208. typedef long intmax_t;
  209. typedef unsigned long uintmax_t;
  210. # else
  211. __extension__
  212. typedef long long intmax_t;
  213. __extension__
  214. typedef unsigned long long uintmax_t;
  215. # endif
  216. #endif
  217. /* Size-saving "small" ints (arch-dependent) */
  218. #if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
  219. /* add other arches which benefit from this... */
  220. typedef signed char smallint;
  221. typedef unsigned char smalluint;
  222. #else
  223. /* for arches where byte accesses generate larger code: */
  224. typedef int smallint;
  225. typedef unsigned smalluint;
  226. #endif
  227. /* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
  228. #if (defined __digital__ && defined __unix__)
  229. /* old system without (proper) C99 support */
  230. # define bool smalluint
  231. #else
  232. /* modern system, so use it */
  233. # include <stdbool.h>
  234. #endif
  235. /* Try to defeat gcc's alignment of "char message[]"-like data */
  236. #if 1 /* if needed: !defined(arch1) && !defined(arch2) */
  237. # define ALIGN1 __attribute__((aligned(1)))
  238. # define ALIGN2 __attribute__((aligned(2)))
  239. #else
  240. /* Arches which MUST have 2 or 4 byte alignment for everything are here */
  241. # define ALIGN1
  242. # define ALIGN2
  243. #endif
  244. /* uclibc does not implement daemon() for no-mmu systems.
  245. * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
  246. * For earlier versions there is no reliable way to check if we are building
  247. * for a mmu-less system.
  248. */
  249. #if ENABLE_NOMMU || \
  250. (defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \
  251. __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__)
  252. # define BB_MMU 0
  253. # define USE_FOR_NOMMU(...) __VA_ARGS__
  254. # define USE_FOR_MMU(...)
  255. #else
  256. # define BB_MMU 1
  257. # define USE_FOR_NOMMU(...)
  258. # define USE_FOR_MMU(...) __VA_ARGS__
  259. #endif
  260. /* Platforms that haven't got dprintf need to implement fdprintf() in
  261. * libbb. This would require a platform.c. It's not going to be cleaned
  262. * out of the tree, so stop saying it should be. */
  263. #if !defined(__dietlibc__)
  264. /* Needed for: glibc */
  265. /* Not needed for: dietlibc */
  266. /* Others: ?? (add as needed) */
  267. # define fdprintf dprintf
  268. #endif
  269. #if defined(__dietlibc__)
  270. static ALWAYS_INLINE char* strchrnul(const char *s, char c)
  271. {
  272. while (*s && *s != c) ++s;
  273. return (char*)s;
  274. }
  275. #endif
  276. /* Don't use lchown with glibc older than 2.1.x */
  277. #if defined(__GLIBC__) && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 1
  278. # define lchown chown
  279. #endif
  280. #if defined(__digital__) && defined(__unix__)
  281. # include <standards.h>
  282. # include <inttypes.h>
  283. # define PRIu32 "u"
  284. /* use legacy setpgrp(pid_t,pid_t) for now. move to platform.c */
  285. # define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me,__me); } while (0)
  286. # if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
  287. # define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
  288. # endif
  289. # if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
  290. # define ADJ_FREQUENCY MOD_FREQUENCY
  291. # endif
  292. # if !defined ADJ_TIMECONST && defined MOD_TIMECONST
  293. # define ADJ_TIMECONST MOD_TIMECONST
  294. # endif
  295. # if !defined ADJ_TICK && defined MOD_CLKB
  296. # define ADJ_TICK MOD_CLKB
  297. # endif
  298. #else
  299. # define bb_setpgrp() setpgrp()
  300. #endif
  301. #endif