dl-string.h 7.5 KB

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