dl-string.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
  4. *
  5. * GNU Lesser General Public License version 2.1 or later.
  6. */
  7. #ifndef _LINUX_STRING_H_
  8. #define _LINUX_STRING_H_
  9. #include <dl-sysdep.h> /* for do_rem */
  10. #include <features.h>
  11. /* provide some sane defaults */
  12. #ifndef do_rem
  13. # define do_rem(result, n, base) ((result) = (n) % (base))
  14. #endif
  15. #ifndef do_div_10
  16. # define do_div_10(result, remain) ((result) /= 10)
  17. #endif
  18. static size_t _dl_strlen(const char *str);
  19. static char *_dl_strcat(char *dst, const char *src);
  20. static char *_dl_strcpy(char *dst, const char *src);
  21. static int _dl_strcmp(const char *s1, const char *s2);
  22. static int _dl_strncmp(const char *s1, const char *s2, size_t len);
  23. static char *_dl_strchr(const char *str, int c);
  24. static char *_dl_strrchr(const char *str, int c);
  25. static char *_dl_strstr(const char *s1, const char *s2);
  26. static void *_dl_memcpy(void *dst, const void *src, size_t len);
  27. static int _dl_memcmp(const void *s1, const void *s2, size_t len);
  28. static void *_dl_memset(void *str, int c, size_t len);
  29. static char *_dl_get_last_path_component(char *path);
  30. static char *_dl_simple_ltoa(char *local, unsigned long i);
  31. static char *_dl_simple_ltoahex(char *local, unsigned long i);
  32. #ifndef NULL
  33. #define NULL ((void *) 0)
  34. #endif
  35. static __always_inline size_t _dl_strlen(const char *str)
  36. {
  37. register const char *ptr = (char *) str-1;
  38. while (*++ptr)
  39. ;/* empty */
  40. return (ptr - str);
  41. }
  42. static __always_inline char * _dl_strcat(char *dst, const char *src)
  43. {
  44. register char *ptr = dst-1;
  45. src--;
  46. while (*++ptr)
  47. ;/* empty */
  48. ptr--;
  49. while ((*++ptr = *++src) != 0)
  50. ;/* empty */
  51. return dst;
  52. }
  53. static __always_inline char * _dl_strcpy(char *dst, const char *src)
  54. {
  55. register char *ptr = dst;
  56. dst--;src--;
  57. while ((*++dst = *++src) != 0)
  58. ;/* empty */
  59. return ptr;
  60. }
  61. static __always_inline int _dl_strcmp(const char *s1, const char *s2)
  62. {
  63. register unsigned char c1, c2;
  64. s1--;s2--;
  65. do {
  66. c1 = (unsigned char) *++s1;
  67. c2 = (unsigned char) *++s2;
  68. if (c1 == '\0')
  69. return c1 - c2;
  70. } while (c1 == c2);
  71. return c1 - c2;
  72. }
  73. static __always_inline int _dl_strncmp(const char *s1, const char *s2, size_t len)
  74. {
  75. register unsigned char c1 = '\0';
  76. register unsigned char c2 = '\0';
  77. s1--;s2--;
  78. while (len > 0) {
  79. c1 = (unsigned char) *++s1;
  80. c2 = (unsigned char) *++s2;
  81. if (c1 == '\0' || c1 != c2)
  82. return c1 - c2;
  83. len--;
  84. }
  85. return c1 - c2;
  86. }
  87. static __always_inline char * _dl_strchr(const char *str, int c)
  88. {
  89. register char ch;
  90. str--;
  91. do {
  92. if ((ch = *++str) == c)
  93. return (char *) str;
  94. }
  95. while (ch);
  96. return 0;
  97. }
  98. static __always_inline char * _dl_strrchr(const char *str, int c)
  99. {
  100. register char *prev = 0;
  101. register char *ptr = (char *) str-1;
  102. while (*++ptr != '\0') {
  103. if (*ptr == c)
  104. prev = ptr;
  105. }
  106. if (c == '\0')
  107. return(ptr);
  108. return(prev);
  109. }
  110. static __always_inline char * _dl_strstr(const char *s1, const char *s2)
  111. {
  112. register const char *s = s1;
  113. register const char *p = s2;
  114. do {
  115. if (!*p)
  116. return (char *) s1;;
  117. if (*p == *s) {
  118. ++p;
  119. ++s;
  120. } else {
  121. p = s2;
  122. if (!*s)
  123. return NULL;
  124. s = ++s1;
  125. }
  126. } while (1);
  127. }
  128. static __always_inline void * _dl_memcpy(void *dst, const void *src, size_t len)
  129. {
  130. register char *a = dst-1;
  131. register const char *b = src-1;
  132. while (len) {
  133. *++a = *++b;
  134. --len;
  135. }
  136. return dst;
  137. }
  138. static __always_inline int _dl_memcmp(const void *s1, const void *s2, size_t len)
  139. {
  140. unsigned char *c1 = (unsigned char *)s1-1;
  141. unsigned char *c2 = (unsigned char *)s2-1;
  142. while (len) {
  143. if (*++c1 != *++c2)
  144. return *c1 - *c2;
  145. len--;
  146. }
  147. return 0;
  148. }
  149. #if defined(powerpc)
  150. /* Will generate smaller and faster code due to loop unrolling.*/
  151. static __always_inline void * _dl_memset(void *to, int c, size_t n)
  152. {
  153. unsigned long chunks;
  154. unsigned long *tmp_to;
  155. unsigned char *tmp_char;
  156. chunks = n / 4;
  157. tmp_to = to + n;
  158. c = c << 8 | c;
  159. c = c << 16 | c;
  160. if (!chunks)
  161. goto lessthan4;
  162. do {
  163. *--tmp_to = c;
  164. } while (--chunks);
  165. lessthan4:
  166. n = n % 4;
  167. if (!n)
  168. return to;
  169. tmp_char = (unsigned char *)tmp_to;
  170. do {
  171. *--tmp_char = c;
  172. } while (--n);
  173. return to;
  174. }
  175. #else
  176. static __always_inline void * _dl_memset(void *str, int c, size_t len)
  177. {
  178. register char *a = str;
  179. while (len--)
  180. *a++ = c;
  181. return str;
  182. }
  183. #endif
  184. static __always_inline char * _dl_get_last_path_component(char *path)
  185. {
  186. register char *ptr = path-1;
  187. while (*++ptr)
  188. ;/* empty */
  189. /* strip trailing slashes */
  190. while (ptr != path && *--ptr == '/') {
  191. *ptr = '\0';
  192. }
  193. /* find last component */
  194. while (ptr != path && *--ptr != '/')
  195. ;/* empty */
  196. return ptr == path ? ptr : ptr+1;
  197. }
  198. /* Early on, we can't call printf, so use this to print out
  199. * numbers using the SEND_STDERR() macro. Avoid using mod
  200. * or using long division */
  201. static __always_inline char * _dl_simple_ltoa(char *local, unsigned long i)
  202. {
  203. /* 20 digits plus a null terminator should be good for
  204. * 64-bit or smaller ints (2^64 - 1)*/
  205. char *p = &local[22];
  206. *--p = '\0';
  207. do {
  208. char temp;
  209. do_rem(temp, i, 10);
  210. *--p = '0' + temp;
  211. do_div_10(i, temp);
  212. } while (i > 0);
  213. return p;
  214. }
  215. static __always_inline char * _dl_simple_ltoahex(char *local, unsigned long i)
  216. {
  217. /* 16 digits plus a leading "0x" plus a null terminator,
  218. * should be good for 64-bit or smaller ints */
  219. char *p = &local[22];
  220. *--p = '\0';
  221. do {
  222. char temp = i & 0xf;
  223. if (temp <= 0x09)
  224. *--p = '0' + temp;
  225. else
  226. *--p = 'a' - 0x0a + temp;
  227. i >>= 4;
  228. } while (i > 0);
  229. *--p = 'x';
  230. *--p = '0';
  231. return p;
  232. }
  233. /* The following macros may be used in dl-startup.c to debug
  234. * ldso before ldso has fixed itself up to make function calls */
  235. /* On some (wierd) arches, none of this stuff works at all, so
  236. * disable the whole lot... */
  237. #if defined(__mips__)
  238. # define SEND_STDERR(X)
  239. # define SEND_ADDRESS_STDERR(X, add_a_newline)
  240. # define SEND_NUMBER_STDERR(X, add_a_newline)
  241. #else
  242. /* On some arches constant strings are referenced through the GOT.
  243. * This requires that load_addr must already be defined... */
  244. #if defined(mc68000) || defined(__arm__) || defined(__thumb__) || \
  245. defined(__mips__) || defined(__sh__) || defined(__powerpc__) || \
  246. defined(__avr32__) || defined(__xtensa__) || defined(__sparc__)
  247. # define CONSTANT_STRING_GOT_FIXUP(X) \
  248. if ((X) < (const char *) load_addr) (X) += load_addr
  249. # define NO_EARLY_SEND_STDERR
  250. #else
  251. # define CONSTANT_STRING_GOT_FIXUP(X)
  252. #endif
  253. #define SEND_STDERR(X) \
  254. { \
  255. const char *tmp1 = (X); \
  256. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  257. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  258. }
  259. #define SEND_ADDRESS_STDERR(ADR, add_a_newline) \
  260. { \
  261. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  262. unsigned long X = (unsigned long)(ADR); \
  263. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  264. tmp2 = tmp1 + sizeof(tmp); \
  265. *--tmp2 = '\0'; \
  266. if (add_a_newline) *--tmp2 = '\n'; \
  267. do { \
  268. v = (X) & 0xf; \
  269. if (v <= 0x09) \
  270. *--tmp2 = '0' + v; \
  271. else \
  272. *--tmp2 = 'a' - 0x0a + v; \
  273. (X) >>= 4; \
  274. } while ((X) > 0); \
  275. *--tmp2 = 'x'; \
  276. *--tmp2 = '0'; \
  277. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  278. }
  279. #define SEND_NUMBER_STDERR(NUM, add_a_newline) \
  280. { \
  281. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  282. unsigned long X = (unsigned long)(NUM); \
  283. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  284. tmp2 = tmp1 + sizeof(tmp); \
  285. *--tmp2 = '\0'; \
  286. if (add_a_newline) *--tmp2 = '\n'; \
  287. do { \
  288. do_rem(v, (X), 10); \
  289. *--tmp2 = '0' + v; \
  290. do_div_10((X), v); \
  291. } while ((X) > 0); \
  292. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  293. }
  294. #endif
  295. /* Some targets may have to override this to something that doesn't
  296. * reference constant strings through the GOT. This macro should be
  297. * preferred over SEND_STDERR for constant strings before we complete
  298. * bootstrap.
  299. */
  300. #ifndef SEND_EARLY_STDERR
  301. # define SEND_EARLY_STDERR(S) SEND_STDERR(S)
  302. #else
  303. # define EARLY_STDERR_SPECIAL
  304. #endif
  305. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  306. # define SEND_STDERR_DEBUG(X) SEND_STDERR(X)
  307. # define SEND_EARLY_STDERR_DEBUG(X) SEND_EARLY_STDERR(X)
  308. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline) SEND_NUMBER_STDERR(X, add_a_newline)
  309. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline) SEND_ADDRESS_STDERR(X, add_a_newline)
  310. #else
  311. # define SEND_STDERR_DEBUG(X)
  312. # define SEND_EARLY_STDERR_DEBUG(X)
  313. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline)
  314. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline)
  315. #endif
  316. #endif