stdlib.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * stdlib function definitions for NOLIBC
  4. * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
  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. #include <linux/auxvec.h>
  14. struct nolibc_heap {
  15. size_t len;
  16. char user_p[] __attribute__((__aligned__));
  17. };
  18. /* Buffer used to store int-to-ASCII conversions. Will only be implemented if
  19. * any of the related functions is implemented. The area is large enough to
  20. * store "18446744073709551615" or "-9223372036854775808" and the final zero.
  21. */
  22. static __attribute__((unused)) char itoa_buffer[21];
  23. /*
  24. * As much as possible, please keep functions alphabetically sorted.
  25. */
  26. /* must be exported, as it's used by libgcc for various divide functions */
  27. __attribute__((weak,unused,noreturn,section(".text.nolibc_abort")))
  28. void abort(void)
  29. {
  30. sys_kill(sys_getpid(), SIGABRT);
  31. for (;;);
  32. }
  33. static __attribute__((unused))
  34. long atol(const char *s)
  35. {
  36. unsigned long ret = 0;
  37. unsigned long d;
  38. int neg = 0;
  39. if (*s == '-') {
  40. neg = 1;
  41. s++;
  42. }
  43. while (1) {
  44. d = (*s++) - '0';
  45. if (d > 9)
  46. break;
  47. ret *= 10;
  48. ret += d;
  49. }
  50. return neg ? -ret : ret;
  51. }
  52. static __attribute__((unused))
  53. int atoi(const char *s)
  54. {
  55. return atol(s);
  56. }
  57. static __attribute__((unused))
  58. void free(void *ptr)
  59. {
  60. struct nolibc_heap *heap;
  61. if (!ptr)
  62. return;
  63. heap = container_of(ptr, struct nolibc_heap, user_p);
  64. munmap(heap, heap->len);
  65. }
  66. /* getenv() tries to find the environment variable named <name> in the
  67. * environment array pointed to by global variable "environ" which must be
  68. * declared as a char **, and must be terminated by a NULL (it is recommended
  69. * to set this variable to the "envp" argument of main()). If the requested
  70. * environment variable exists its value is returned otherwise NULL is
  71. * returned.
  72. */
  73. static __attribute__((unused))
  74. char *getenv(const char *name)
  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 __attribute__((unused))
  88. unsigned long getauxval(unsigned long type)
  89. {
  90. const unsigned long *auxv = _auxv;
  91. unsigned long ret;
  92. if (!auxv)
  93. return 0;
  94. while (1) {
  95. if (!auxv[0] && !auxv[1]) {
  96. ret = 0;
  97. break;
  98. }
  99. if (auxv[0] == type) {
  100. ret = auxv[1];
  101. break;
  102. }
  103. auxv += 2;
  104. }
  105. return ret;
  106. }
  107. static __attribute__((unused))
  108. void *malloc(size_t len)
  109. {
  110. struct nolibc_heap *heap;
  111. /* Always allocate memory with size multiple of 4096. */
  112. len = sizeof(*heap) + len;
  113. len = (len + 4095UL) & -4096UL;
  114. heap = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
  115. -1, 0);
  116. if (__builtin_expect(heap == MAP_FAILED, 0))
  117. return NULL;
  118. heap->len = len;
  119. return heap->user_p;
  120. }
  121. static __attribute__((unused))
  122. void *calloc(size_t size, size_t nmemb)
  123. {
  124. size_t x = size * nmemb;
  125. if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
  126. SET_ERRNO(ENOMEM);
  127. return NULL;
  128. }
  129. /*
  130. * No need to zero the heap, the MAP_ANONYMOUS in malloc()
  131. * already does it.
  132. */
  133. return malloc(x);
  134. }
  135. static __attribute__((unused))
  136. void *realloc(void *old_ptr, size_t new_size)
  137. {
  138. struct nolibc_heap *heap;
  139. size_t user_p_len;
  140. void *ret;
  141. if (!old_ptr)
  142. return malloc(new_size);
  143. heap = container_of(old_ptr, struct nolibc_heap, user_p);
  144. user_p_len = heap->len - sizeof(*heap);
  145. /*
  146. * Don't realloc() if @user_p_len >= @new_size, this block of
  147. * memory is still enough to handle the @new_size. Just return
  148. * the same pointer.
  149. */
  150. if (user_p_len >= new_size)
  151. return old_ptr;
  152. ret = malloc(new_size);
  153. if (__builtin_expect(!ret, 0))
  154. return NULL;
  155. memcpy(ret, heap->user_p, user_p_len);
  156. munmap(heap, heap->len);
  157. return ret;
  158. }
  159. /* Converts the unsigned long integer <in> to its hex representation into
  160. * buffer <buffer>, which must be long enough to store the number and the
  161. * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
  162. * buffer is filled from the first byte, and the number of characters emitted
  163. * (not counting the trailing zero) is returned. The function is constructed
  164. * in a way to optimize the code size and avoid any divide that could add a
  165. * dependency on large external functions.
  166. */
  167. static __attribute__((unused))
  168. int utoh_r(unsigned long in, char *buffer)
  169. {
  170. signed char pos = (~0UL > 0xfffffffful) ? 60 : 28;
  171. int digits = 0;
  172. int dig;
  173. do {
  174. dig = in >> pos;
  175. in -= (uint64_t)dig << pos;
  176. pos -= 4;
  177. if (dig || digits || pos < 0) {
  178. if (dig > 9)
  179. dig += 'a' - '0' - 10;
  180. buffer[digits++] = '0' + dig;
  181. }
  182. } while (pos >= 0);
  183. buffer[digits] = 0;
  184. return digits;
  185. }
  186. /* converts unsigned long <in> to an hex string using the static itoa_buffer
  187. * and returns the pointer to that string.
  188. */
  189. static __inline__ __attribute__((unused))
  190. char *utoh(unsigned long in)
  191. {
  192. utoh_r(in, itoa_buffer);
  193. return itoa_buffer;
  194. }
  195. /* Converts the unsigned long integer <in> to its string representation into
  196. * buffer <buffer>, which must be long enough to store the number and the
  197. * trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for
  198. * 4294967295 in 32-bit). The buffer is filled from the first byte, and the
  199. * number of characters emitted (not counting the trailing zero) is returned.
  200. * The function is constructed in a way to optimize the code size and avoid
  201. * any divide that could add a dependency on large external functions.
  202. */
  203. static __attribute__((unused))
  204. int utoa_r(unsigned long in, char *buffer)
  205. {
  206. unsigned long lim;
  207. int digits = 0;
  208. int pos = (~0UL > 0xfffffffful) ? 19 : 9;
  209. int dig;
  210. do {
  211. for (dig = 0, lim = 1; dig < pos; dig++)
  212. lim *= 10;
  213. if (digits || in >= lim || !pos) {
  214. for (dig = 0; in >= lim; dig++)
  215. in -= lim;
  216. buffer[digits++] = '0' + dig;
  217. }
  218. } while (pos--);
  219. buffer[digits] = 0;
  220. return digits;
  221. }
  222. /* Converts the signed long integer <in> to its string representation into
  223. * buffer <buffer>, which must be long enough to store the number and the
  224. * trailing zero (21 bytes for -9223372036854775808 in 64-bit, 12 for
  225. * -2147483648 in 32-bit). The buffer is filled from the first byte, and the
  226. * number of characters emitted (not counting the trailing zero) is returned.
  227. */
  228. static __attribute__((unused))
  229. int itoa_r(long in, char *buffer)
  230. {
  231. char *ptr = buffer;
  232. int len = 0;
  233. if (in < 0) {
  234. in = -in;
  235. *(ptr++) = '-';
  236. len++;
  237. }
  238. len += utoa_r(in, ptr);
  239. return len;
  240. }
  241. /* for historical compatibility, same as above but returns the pointer to the
  242. * buffer.
  243. */
  244. static __inline__ __attribute__((unused))
  245. char *ltoa_r(long in, char *buffer)
  246. {
  247. itoa_r(in, buffer);
  248. return buffer;
  249. }
  250. /* converts long integer <in> to a string using the static itoa_buffer and
  251. * returns the pointer to that string.
  252. */
  253. static __inline__ __attribute__((unused))
  254. char *itoa(long in)
  255. {
  256. itoa_r(in, itoa_buffer);
  257. return itoa_buffer;
  258. }
  259. /* converts long integer <in> to a string using the static itoa_buffer and
  260. * returns the pointer to that string. Same as above, for compatibility.
  261. */
  262. static __inline__ __attribute__((unused))
  263. char *ltoa(long in)
  264. {
  265. itoa_r(in, itoa_buffer);
  266. return itoa_buffer;
  267. }
  268. /* converts unsigned long integer <in> to a string using the static itoa_buffer
  269. * and returns the pointer to that string.
  270. */
  271. static __inline__ __attribute__((unused))
  272. char *utoa(unsigned long in)
  273. {
  274. utoa_r(in, itoa_buffer);
  275. return itoa_buffer;
  276. }
  277. /* Converts the unsigned 64-bit integer <in> to its hex representation into
  278. * buffer <buffer>, which must be long enough to store the number and the
  279. * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from
  280. * the first byte, and the number of characters emitted (not counting the
  281. * trailing zero) is returned. The function is constructed in a way to optimize
  282. * the code size and avoid any divide that could add a dependency on large
  283. * external functions.
  284. */
  285. static __attribute__((unused))
  286. int u64toh_r(uint64_t in, char *buffer)
  287. {
  288. signed char pos = 60;
  289. int digits = 0;
  290. int dig;
  291. do {
  292. if (sizeof(long) >= 8) {
  293. dig = (in >> pos) & 0xF;
  294. } else {
  295. /* 32-bit platforms: avoid a 64-bit shift */
  296. uint32_t d = (pos >= 32) ? (in >> 32) : in;
  297. dig = (d >> (pos & 31)) & 0xF;
  298. }
  299. if (dig > 9)
  300. dig += 'a' - '0' - 10;
  301. pos -= 4;
  302. if (dig || digits || pos < 0)
  303. buffer[digits++] = '0' + dig;
  304. } while (pos >= 0);
  305. buffer[digits] = 0;
  306. return digits;
  307. }
  308. /* converts uint64_t <in> to an hex string using the static itoa_buffer and
  309. * returns the pointer to that string.
  310. */
  311. static __inline__ __attribute__((unused))
  312. char *u64toh(uint64_t in)
  313. {
  314. u64toh_r(in, itoa_buffer);
  315. return itoa_buffer;
  316. }
  317. /* Converts the unsigned 64-bit integer <in> to its string representation into
  318. * buffer <buffer>, which must be long enough to store the number and the
  319. * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
  320. * the first byte, and the number of characters emitted (not counting the
  321. * trailing zero) is returned. The function is constructed in a way to optimize
  322. * the code size and avoid any divide that could add a dependency on large
  323. * external functions.
  324. */
  325. static __attribute__((unused))
  326. int u64toa_r(uint64_t in, char *buffer)
  327. {
  328. unsigned long long lim;
  329. int digits = 0;
  330. int pos = 19; /* start with the highest possible digit */
  331. int dig;
  332. do {
  333. for (dig = 0, lim = 1; dig < pos; dig++)
  334. lim *= 10;
  335. if (digits || in >= lim || !pos) {
  336. for (dig = 0; in >= lim; dig++)
  337. in -= lim;
  338. buffer[digits++] = '0' + dig;
  339. }
  340. } while (pos--);
  341. buffer[digits] = 0;
  342. return digits;
  343. }
  344. /* Converts the signed 64-bit integer <in> to its string representation into
  345. * buffer <buffer>, which must be long enough to store the number and the
  346. * trailing zero (21 bytes for -9223372036854775808). The buffer is filled from
  347. * the first byte, and the number of characters emitted (not counting the
  348. * trailing zero) is returned.
  349. */
  350. static __attribute__((unused))
  351. int i64toa_r(int64_t in, char *buffer)
  352. {
  353. char *ptr = buffer;
  354. int len = 0;
  355. if (in < 0) {
  356. in = -in;
  357. *(ptr++) = '-';
  358. len++;
  359. }
  360. len += u64toa_r(in, ptr);
  361. return len;
  362. }
  363. /* converts int64_t <in> to a string using the static itoa_buffer and returns
  364. * the pointer to that string.
  365. */
  366. static __inline__ __attribute__((unused))
  367. char *i64toa(int64_t in)
  368. {
  369. i64toa_r(in, itoa_buffer);
  370. return itoa_buffer;
  371. }
  372. /* converts uint64_t <in> to a string using the static itoa_buffer and returns
  373. * the pointer to that string.
  374. */
  375. static __inline__ __attribute__((unused))
  376. char *u64toa(uint64_t in)
  377. {
  378. u64toa_r(in, itoa_buffer);
  379. return itoa_buffer;
  380. }
  381. static __attribute__((unused))
  382. uintmax_t __strtox(const char *nptr, char **endptr, int base, intmax_t lower_limit, uintmax_t upper_limit)
  383. {
  384. const char signed_ = lower_limit != 0;
  385. unsigned char neg = 0, overflow = 0;
  386. uintmax_t val = 0, limit, old_val;
  387. char c;
  388. if (base < 0 || base > 36) {
  389. SET_ERRNO(EINVAL);
  390. goto out;
  391. }
  392. while (isspace(*nptr))
  393. nptr++;
  394. if (*nptr == '+') {
  395. nptr++;
  396. } else if (*nptr == '-') {
  397. neg = 1;
  398. nptr++;
  399. }
  400. if (signed_ && neg)
  401. limit = -(uintmax_t)lower_limit;
  402. else
  403. limit = upper_limit;
  404. if ((base == 0 || base == 16) &&
  405. (strncmp(nptr, "0x", 2) == 0 || strncmp(nptr, "0X", 2) == 0)) {
  406. base = 16;
  407. nptr += 2;
  408. } else if (base == 0 && strncmp(nptr, "0", 1) == 0) {
  409. base = 8;
  410. nptr += 1;
  411. } else if (base == 0) {
  412. base = 10;
  413. }
  414. while (*nptr) {
  415. c = *nptr;
  416. if (c >= '0' && c <= '9')
  417. c -= '0';
  418. else if (c >= 'a' && c <= 'z')
  419. c = c - 'a' + 10;
  420. else if (c >= 'A' && c <= 'Z')
  421. c = c - 'A' + 10;
  422. else
  423. goto out;
  424. if (c >= base)
  425. goto out;
  426. nptr++;
  427. old_val = val;
  428. val *= base;
  429. val += c;
  430. if (val > limit || val < old_val)
  431. overflow = 1;
  432. }
  433. out:
  434. if (overflow) {
  435. SET_ERRNO(ERANGE);
  436. val = limit;
  437. }
  438. if (endptr)
  439. *endptr = (char *)nptr;
  440. return neg ? -val : val;
  441. }
  442. static __attribute__((unused))
  443. long strtol(const char *nptr, char **endptr, int base)
  444. {
  445. return __strtox(nptr, endptr, base, LONG_MIN, LONG_MAX);
  446. }
  447. static __attribute__((unused))
  448. unsigned long strtoul(const char *nptr, char **endptr, int base)
  449. {
  450. return __strtox(nptr, endptr, base, 0, ULONG_MAX);
  451. }
  452. static __attribute__((unused))
  453. long long strtoll(const char *nptr, char **endptr, int base)
  454. {
  455. return __strtox(nptr, endptr, base, LLONG_MIN, LLONG_MAX);
  456. }
  457. static __attribute__((unused))
  458. unsigned long long strtoull(const char *nptr, char **endptr, int base)
  459. {
  460. return __strtox(nptr, endptr, base, 0, ULLONG_MAX);
  461. }
  462. static __attribute__((unused))
  463. intmax_t strtoimax(const char *nptr, char **endptr, int base)
  464. {
  465. return __strtox(nptr, endptr, base, INTMAX_MIN, INTMAX_MAX);
  466. }
  467. static __attribute__((unused))
  468. uintmax_t strtoumax(const char *nptr, char **endptr, int base)
  469. {
  470. return __strtox(nptr, endptr, base, 0, UINTMAX_MAX);
  471. }
  472. /* make sure to include all global symbols */
  473. #include "nolibc.h"
  474. #endif /* _NOLIBC_STDLIB_H */