dl-string.h 7.4 KB

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