stdlib.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * stdlib function definitions for NOLIBC
  4. * Copyright (C) 2017-2021 Willy Tarreau <[email protected]>
  5. */
  6. #ifndef _NOLIBC_STDLIB_H
  7. #define _NOLIBC_STDLIB_H
  8. #include "std.h"
  9. #include "arch.h"
  10. #include "types.h"
  11. #include "sys.h"
  12. #include "string.h"
  13. struct nolibc_heap {
  14. size_t len;
  15. char user_p[] __attribute__((__aligned__));
  16. };
  17. /* Buffer used to store int-to-ASCII conversions. Will only be implemented if
  18. * any of the related functions is implemented. The area is large enough to
  19. * store "18446744073709551615" or "-9223372036854775808" and the final zero.
  20. */
  21. static __attribute__((unused)) char itoa_buffer[21];
  22. /*
  23. * As much as possible, please keep functions alphabetically sorted.
  24. */
  25. /* must be exported, as it's used by libgcc for various divide functions */
  26. __attribute__((weak,unused,noreturn,section(".text.nolibc_abort")))
  27. void abort(void)
  28. {
  29. sys_kill(sys_getpid(), SIGABRT);
  30. for (;;);
  31. }
  32. static __attribute__((unused))
  33. long atol(const char *s)
  34. {
  35. unsigned long ret = 0;
  36. unsigned long d;
  37. int neg = 0;
  38. if (*s == '-') {
  39. neg = 1;
  40. s++;
  41. }
  42. while (1) {
  43. d = (*s++) - '0';
  44. if (d > 9)
  45. break;
  46. ret *= 10;
  47. ret += d;
  48. }
  49. return neg ? -ret : ret;
  50. }
  51. static __attribute__((unused))
  52. int atoi(const char *s)
  53. {
  54. return atol(s);
  55. }
  56. static __attribute__((unused))
  57. void free(void *ptr)
  58. {
  59. struct nolibc_heap *heap;
  60. if (!ptr)
  61. return;
  62. heap = container_of(ptr, struct nolibc_heap, user_p);
  63. munmap(heap, heap->len);
  64. }
  65. /* getenv() tries to find the environment variable named <name> in the
  66. * environment array pointed to by global variable "environ" which must be
  67. * declared as a char **, and must be terminated by a NULL (it is recommended
  68. * to set this variable to the "envp" argument of main()). If the requested
  69. * environment variable exists its value is returned otherwise NULL is
  70. * returned. getenv() is forcefully inlined so that the reference to "environ"
  71. * will be dropped if unused, even at -O0.
  72. */
  73. static __attribute__((unused))
  74. char *_getenv(const char *name, char **environ)
  75. {
  76. int idx, i;
  77. if (environ) {
  78. for (idx = 0; environ[idx]; idx++) {
  79. for (i = 0; name[i] && name[i] == environ[idx][i];)
  80. i++;
  81. if (!name[i] && environ[idx][i] == '=')
  82. return &environ[idx][i+1];
  83. }
  84. }
  85. return NULL;
  86. }
  87. static inline __attribute__((unused,always_inline))
  88. char *getenv(const char *name)
  89. {
  90. extern char **environ;
  91. return _getenv(name, environ);
  92. }
  93. static __attribute__((unused))
  94. void *malloc(size_t len)
  95. {
  96. struct nolibc_heap *heap;
  97. /* Always allocate memory with size multiple of 4096. */
  98. len = sizeof(*heap) + len;
  99. len = (len + 4095UL) & -4096UL;
  100. heap = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
  101. -1, 0);
  102. if (__builtin_expect(heap == MAP_FAILED, 0))
  103. return NULL;
  104. heap->len = len;
  105. return heap->user_p;
  106. }
  107. static __attribute__((unused))
  108. void *calloc(size_t size, size_t nmemb)
  109. {
  110. size_t x = size * nmemb;
  111. if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
  112. SET_ERRNO(ENOMEM);
  113. return NULL;
  114. }
  115. /*
  116. * No need to zero the heap, the MAP_ANONYMOUS in malloc()
  117. * already does it.
  118. */
  119. return malloc(x);
  120. }
  121. static __attribute__((unused))
  122. void *realloc(void *old_ptr, size_t new_size)
  123. {
  124. struct nolibc_heap *heap;
  125. size_t user_p_len;
  126. void *ret;
  127. if (!old_ptr)
  128. return malloc(new_size);
  129. heap = container_of(old_ptr, struct nolibc_heap, user_p);
  130. user_p_len = heap->len - sizeof(*heap);
  131. /*
  132. * Don't realloc() if @user_p_len >= @new_size, this block of
  133. * memory is still enough to handle the @new_size. Just return
  134. * the same pointer.
  135. */
  136. if (user_p_len >= new_size)
  137. return old_ptr;
  138. ret = malloc(new_size);
  139. if (__builtin_expect(!ret, 0))
  140. return NULL;
  141. memcpy(ret, heap->user_p, heap->len);
  142. munmap(heap, heap->len);
  143. return ret;
  144. }
  145. /* Converts the unsigned long integer <in> to its hex representation into
  146. * buffer <buffer>, which must be long enough to store the number and the
  147. * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
  148. * buffer is filled from the first byte, and the number of characters emitted
  149. * (not counting the trailing zero) is returned. The function is constructed
  150. * in a way to optimize the code size and avoid any divide that could add a
  151. * dependency on large external functions.
  152. */
  153. static __attribute__((unused))
  154. int utoh_r(unsigned long in, char *buffer)
  155. {
  156. signed char pos = (~0UL > 0xfffffffful) ? 60 : 28;
  157. int digits = 0;
  158. int dig;
  159. do {
  160. dig = in >> pos;
  161. in -= (uint64_t)dig << pos;
  162. pos -= 4;
  163. if (dig || digits || pos < 0) {
  164. if (dig > 9)
  165. dig += 'a' - '0' - 10;
  166. buffer[digits++] = '0' + dig;
  167. }
  168. } while (pos >= 0);
  169. buffer[digits] = 0;
  170. return digits;
  171. }
  172. /* converts unsigned long <in> to an hex string using the static itoa_buffer
  173. * and returns the pointer to that string.
  174. */
  175. static inline __attribute__((unused))
  176. char *utoh(unsigned long in)
  177. {
  178. utoh_r(in, itoa_buffer);
  179. return itoa_buffer;
  180. }
  181. /* Converts the unsigned long integer <in> to its string representation into
  182. * buffer <buffer>, which must be long enough to store the number and the
  183. * trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for
  184. * 4294967295 in 32-bit). The buffer is filled from the first byte, and the
  185. * number of characters emitted (not counting the trailing zero) is returned.
  186. * The function is constructed in a way to optimize the code size and avoid
  187. * any divide that could add a dependency on large external functions.
  188. */
  189. static __attribute__((unused))
  190. int utoa_r(unsigned long in, char *buffer)
  191. {
  192. unsigned long lim;
  193. int digits = 0;
  194. int pos = (~0UL > 0xfffffffful) ? 19 : 9;
  195. int dig;
  196. do {
  197. for (dig = 0, lim = 1; dig < pos; dig++)
  198. lim *= 10;
  199. if (digits || in >= lim || !pos) {
  200. for (dig = 0; in >= lim; dig++)
  201. in -= lim;
  202. buffer[digits++] = '0' + dig;
  203. }
  204. } while (pos--);
  205. buffer[digits] = 0;
  206. return digits;
  207. }
  208. /* Converts the signed long integer <in> to its string representation into
  209. * buffer <buffer>, which must be long enough to store the number and the
  210. * trailing zero (21 bytes for -9223372036854775808 in 64-bit, 12 for
  211. * -2147483648 in 32-bit). The buffer is filled from the first byte, and the
  212. * number of characters emitted (not counting the trailing zero) is returned.
  213. */
  214. static __attribute__((unused))
  215. int itoa_r(long in, char *buffer)
  216. {
  217. char *ptr = buffer;
  218. int len = 0;
  219. if (in < 0) {
  220. in = -in;
  221. *(ptr++) = '-';
  222. len++;
  223. }
  224. len += utoa_r(in, ptr);
  225. return len;
  226. }
  227. /* for historical compatibility, same as above but returns the pointer to the
  228. * buffer.
  229. */
  230. static inline __attribute__((unused))
  231. char *ltoa_r(long in, char *buffer)
  232. {
  233. itoa_r(in, buffer);
  234. return buffer;
  235. }
  236. /* converts long integer <in> to a string using the static itoa_buffer and
  237. * returns the pointer to that string.
  238. */
  239. static inline __attribute__((unused))
  240. char *itoa(long in)
  241. {
  242. itoa_r(in, itoa_buffer);
  243. return itoa_buffer;
  244. }
  245. /* converts long integer <in> to a string using the static itoa_buffer and
  246. * returns the pointer to that string. Same as above, for compatibility.
  247. */
  248. static inline __attribute__((unused))
  249. char *ltoa(long in)
  250. {
  251. itoa_r(in, itoa_buffer);
  252. return itoa_buffer;
  253. }
  254. /* converts unsigned long integer <in> to a string using the static itoa_buffer
  255. * and returns the pointer to that string.
  256. */
  257. static inline __attribute__((unused))
  258. char *utoa(unsigned long in)
  259. {
  260. utoa_r(in, itoa_buffer);
  261. return itoa_buffer;
  262. }
  263. /* Converts the unsigned 64-bit integer <in> to its hex representation into
  264. * buffer <buffer>, which must be long enough to store the number and the
  265. * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from
  266. * the first byte, and the number of characters emitted (not counting the
  267. * trailing zero) is returned. The function is constructed in a way to optimize
  268. * the code size and avoid any divide that could add a dependency on large
  269. * external functions.
  270. */
  271. static __attribute__((unused))
  272. int u64toh_r(uint64_t in, char *buffer)
  273. {
  274. signed char pos = 60;
  275. int digits = 0;
  276. int dig;
  277. do {
  278. if (sizeof(long) >= 8) {
  279. dig = (in >> pos) & 0xF;
  280. } else {
  281. /* 32-bit platforms: avoid a 64-bit shift */
  282. uint32_t d = (pos >= 32) ? (in >> 32) : in;
  283. dig = (d >> (pos & 31)) & 0xF;
  284. }
  285. if (dig > 9)
  286. dig += 'a' - '0' - 10;
  287. pos -= 4;
  288. if (dig || digits || pos < 0)
  289. buffer[digits++] = '0' + dig;
  290. } while (pos >= 0);
  291. buffer[digits] = 0;
  292. return digits;
  293. }
  294. /* converts uint64_t <in> to an hex string using the static itoa_buffer and
  295. * returns the pointer to that string.
  296. */
  297. static inline __attribute__((unused))
  298. char *u64toh(uint64_t in)
  299. {
  300. u64toh_r(in, itoa_buffer);
  301. return itoa_buffer;
  302. }
  303. /* Converts the unsigned 64-bit integer <in> to its string representation into
  304. * buffer <buffer>, which must be long enough to store the number and the
  305. * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
  306. * the first byte, and the number of characters emitted (not counting the
  307. * trailing zero) is returned. The function is constructed in a way to optimize
  308. * the code size and avoid any divide that could add a dependency on large
  309. * external functions.
  310. */
  311. static __attribute__((unused))
  312. int u64toa_r(uint64_t in, char *buffer)
  313. {
  314. unsigned long long lim;
  315. int digits = 0;
  316. int pos = 19; /* start with the highest possible digit */
  317. int dig;
  318. do {
  319. for (dig = 0, lim = 1; dig < pos; dig++)
  320. lim *= 10;
  321. if (digits || in >= lim || !pos) {
  322. for (dig = 0; in >= lim; dig++)
  323. in -= lim;
  324. buffer[digits++] = '0' + dig;
  325. }
  326. } while (pos--);
  327. buffer[digits] = 0;
  328. return digits;
  329. }
  330. /* Converts the signed 64-bit integer <in> to its string representation into
  331. * buffer <buffer>, which must be long enough to store the number and the
  332. * trailing zero (21 bytes for -9223372036854775808). The buffer is filled from
  333. * the first byte, and the number of characters emitted (not counting the
  334. * trailing zero) is returned.
  335. */
  336. static __attribute__((unused))
  337. int i64toa_r(int64_t in, char *buffer)
  338. {
  339. char *ptr = buffer;
  340. int len = 0;
  341. if (in < 0) {
  342. in = -in;
  343. *(ptr++) = '-';
  344. len++;
  345. }
  346. len += u64toa_r(in, ptr);
  347. return len;
  348. }
  349. /* converts int64_t <in> to a string using the static itoa_buffer and returns
  350. * the pointer to that string.
  351. */
  352. static inline __attribute__((unused))
  353. char *i64toa(int64_t in)
  354. {
  355. i64toa_r(in, itoa_buffer);
  356. return itoa_buffer;
  357. }
  358. /* converts uint64_t <in> to a string using the static itoa_buffer and returns
  359. * the pointer to that string.
  360. */
  361. static inline __attribute__((unused))
  362. char *u64toa(uint64_t in)
  363. {
  364. u64toa_r(in, itoa_buffer);
  365. return itoa_buffer;
  366. }
  367. /* make sure to include all global symbols */
  368. #include "nolibc.h"
  369. #endif /* _NOLIBC_STDLIB_H */