dl-string.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_strrchr strrchr
  177. # define _dl_memcpy memcpy
  178. # define _dl_memcmp memcmp
  179. # define _dl_memset memset
  180. #endif /* IS_IN_rtld */
  181. #if defined IS_IN_rtld || defined __SUPPORT_LD_DEBUG__
  182. /* Early on, we can't call printf, so use this to print out
  183. * numbers using the SEND_STDERR() macro. Avoid using mod
  184. * or using long division */
  185. static __always_inline char * _dl_simple_ltoa(char *local, unsigned long i)
  186. {
  187. /* 20 digits plus a null terminator should be good for
  188. * 64-bit or smaller ints (2^64 - 1)*/
  189. char *p = &local[22];
  190. *--p = '\0';
  191. do {
  192. char temp;
  193. do_rem(temp, i, 10);
  194. *--p = '0' + temp;
  195. do_div_10(i, temp);
  196. } while (i > 0);
  197. return p;
  198. }
  199. #endif
  200. #ifdef IS_IN_rtld
  201. static __always_inline char * _dl_simple_ltoahex(char *local, unsigned long i)
  202. {
  203. /* 16 digits plus a leading "0x" plus a null terminator,
  204. * should be good for 64-bit or smaller ints */
  205. char *p = &local[22];
  206. *--p = '\0';
  207. do {
  208. char temp = i & 0xf;
  209. if (temp <= 0x09)
  210. *--p = '0' + temp;
  211. else
  212. *--p = 'a' - 0x0a + temp;
  213. i >>= 4;
  214. } while (i > 0);
  215. *--p = 'x';
  216. *--p = '0';
  217. return p;
  218. }
  219. /* The following macros may be used in dl-startup.c to debug
  220. * ldso before ldso has fixed itself up to make function calls */
  221. /* On some (wierd) arches, none of this stuff works at all, so
  222. * disable the whole lot... */
  223. #if defined(__mips__)
  224. # define SEND_STDERR(X)
  225. # define SEND_ADDRESS_STDERR(X, add_a_newline)
  226. # define SEND_NUMBER_STDERR(X, add_a_newline)
  227. #else
  228. /* On some arches constant strings are referenced through the GOT.
  229. * This requires that load_addr must already be defined... */
  230. #if defined(mc68000) || defined(__arm__) || defined(__thumb__) || \
  231. defined(__sh__) || defined(__powerpc__) || \
  232. defined(__avr32__) || defined(__xtensa__) || defined(__sparc__) || defined(__microblaze__)
  233. # define CONSTANT_STRING_GOT_FIXUP(X) \
  234. if ((X) < (const char *) load_addr) (X) += load_addr
  235. # define NO_EARLY_SEND_STDERR
  236. #else
  237. # define CONSTANT_STRING_GOT_FIXUP(X)
  238. #endif
  239. #define SEND_STDERR(X) \
  240. { \
  241. const char *tmp1 = (X); \
  242. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  243. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  244. }
  245. #define SEND_ADDRESS_STDERR(ADR, add_a_newline) \
  246. { \
  247. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  248. unsigned long X = (unsigned long)(ADR); \
  249. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  250. tmp2 = tmp1 + sizeof(tmp); \
  251. *--tmp2 = '\0'; \
  252. if (add_a_newline) *--tmp2 = '\n'; \
  253. do { \
  254. v = (X) & 0xf; \
  255. if (v <= 0x09) \
  256. *--tmp2 = '0' + v; \
  257. else \
  258. *--tmp2 = 'a' - 0x0a + v; \
  259. (X) >>= 4; \
  260. } while ((X) > 0); \
  261. *--tmp2 = 'x'; \
  262. *--tmp2 = '0'; \
  263. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  264. }
  265. #define SEND_NUMBER_STDERR(NUM, add_a_newline) \
  266. { \
  267. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  268. unsigned long X = (unsigned long)(NUM); \
  269. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  270. tmp2 = tmp1 + sizeof(tmp); \
  271. *--tmp2 = '\0'; \
  272. if (add_a_newline) *--tmp2 = '\n'; \
  273. do { \
  274. do_rem(v, (X), 10); \
  275. *--tmp2 = '0' + v; \
  276. do_div_10((X), v); \
  277. } while ((X) > 0); \
  278. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  279. }
  280. #endif
  281. /* Some targets may have to override this to something that doesn't
  282. * reference constant strings through the GOT. This macro should be
  283. * preferred over SEND_STDERR for constant strings before we complete
  284. * bootstrap.
  285. */
  286. #ifndef SEND_EARLY_STDERR
  287. # define SEND_EARLY_STDERR(S) SEND_STDERR(S)
  288. #else
  289. # define EARLY_STDERR_SPECIAL
  290. #endif
  291. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  292. # define SEND_STDERR_DEBUG(X) SEND_STDERR(X)
  293. # define SEND_EARLY_STDERR_DEBUG(X) SEND_EARLY_STDERR(X)
  294. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline) SEND_NUMBER_STDERR(X, add_a_newline)
  295. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline) SEND_ADDRESS_STDERR(X, add_a_newline)
  296. #else
  297. # define SEND_STDERR_DEBUG(X)
  298. # define SEND_EARLY_STDERR_DEBUG(X)
  299. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline)
  300. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline)
  301. #endif
  302. #endif /* IS_IN_rtld */
  303. #endif /* _DL_STRING_H */