dl-string.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
  3. *
  4. * GNU Lesser General Public License version 2.1 or later.
  5. */
  6. #ifndef _DL_STRING_H
  7. #define _DL_STRING_H
  8. #include <features.h>
  9. #define __need_NULL
  10. #include <stddef.h>
  11. #include <dl-defs.h> /* for do_rem by dl-sysdep.h */
  12. /* provide some sane defaults */
  13. #ifndef do_rem
  14. # define do_rem(result, n, base) ((result) = (n) % (base))
  15. #endif
  16. #ifndef do_div_10
  17. # define do_div_10(result, remain) ((result) /= 10)
  18. #endif
  19. #ifdef IS_IN_rtld
  20. static __always_inline size_t _dl_strlen(const char *str)
  21. {
  22. register const char *ptr = (char *) str-1;
  23. while (*++ptr)
  24. ;/* empty */
  25. return (ptr - str);
  26. }
  27. static __always_inline char * _dl_strcat(char *dst, const char *src)
  28. {
  29. register char *ptr = dst-1;
  30. src--;
  31. while (*++ptr)
  32. ;/* empty */
  33. ptr--;
  34. while ((*++ptr = *++src) != 0)
  35. ;/* empty */
  36. return dst;
  37. }
  38. static __always_inline char * _dl_strcpy(char *dst, const char *src)
  39. {
  40. register char *ptr = dst;
  41. dst--;src--;
  42. while ((*++dst = *++src) != 0)
  43. ;/* empty */
  44. return ptr;
  45. }
  46. static __always_inline int _dl_strcmp(const char *s1, const char *s2)
  47. {
  48. register unsigned char c1, c2;
  49. s1--;s2--;
  50. do {
  51. c1 = (unsigned char) *++s1;
  52. c2 = (unsigned char) *++s2;
  53. if (c1 == '\0')
  54. return c1 - c2;
  55. } while (c1 == c2);
  56. return c1 - c2;
  57. }
  58. static __always_inline char * _dl_strchr(const char *str, int c)
  59. {
  60. register char ch;
  61. str--;
  62. do {
  63. if ((ch = *++str) == c)
  64. return (char *) str;
  65. }
  66. while (ch);
  67. return 0;
  68. }
  69. static __always_inline char * _dl_strrchr(const char *str, int c)
  70. {
  71. register char *prev = 0;
  72. register char *ptr = (char *) str-1;
  73. while (*++ptr != '\0') {
  74. if (*ptr == c)
  75. prev = ptr;
  76. }
  77. if (c == '\0')
  78. return(ptr);
  79. return(prev);
  80. }
  81. static __always_inline char * _dl_strstr(const char *s1, const char *s2)
  82. {
  83. register const char *s = s1;
  84. register const char *p = s2;
  85. do {
  86. if (!*p)
  87. return (char *) s1;;
  88. if (*p == *s) {
  89. ++p;
  90. ++s;
  91. } else {
  92. p = s2;
  93. if (!*s)
  94. return NULL;
  95. s = ++s1;
  96. }
  97. } while (1);
  98. }
  99. static __always_inline void * _dl_memcpy(void *dst, const void *src, size_t len)
  100. {
  101. register char *a = dst-1;
  102. register const char *b = src-1;
  103. while (len) {
  104. *++a = *++b;
  105. --len;
  106. }
  107. return dst;
  108. }
  109. static __always_inline int _dl_memcmp(const void *s1, const void *s2, size_t len)
  110. {
  111. unsigned char *c1 = (unsigned char *)s1-1;
  112. unsigned char *c2 = (unsigned char *)s2-1;
  113. while (len) {
  114. if (*++c1 != *++c2)
  115. return *c1 - *c2;
  116. len--;
  117. }
  118. return 0;
  119. }
  120. #if defined(__powerpc__)
  121. /* Will generate smaller and faster code due to loop unrolling.*/
  122. static __always_inline void * _dl_memset(void *to, int c, size_t n)
  123. {
  124. unsigned long chunks;
  125. unsigned long *tmp_to;
  126. unsigned char *tmp_char;
  127. chunks = n / 4;
  128. tmp_to = to + n;
  129. c = c << 8 | c;
  130. c = c << 16 | c;
  131. if (!chunks)
  132. goto lessthan4;
  133. do {
  134. *--tmp_to = c;
  135. } while (--chunks);
  136. lessthan4:
  137. n = n % 4;
  138. if (!n)
  139. return to;
  140. tmp_char = (unsigned char *)tmp_to;
  141. do {
  142. *--tmp_char = c;
  143. } while (--n);
  144. return to;
  145. }
  146. #else
  147. static __always_inline void * _dl_memset(void *str, int c, size_t len)
  148. {
  149. register char *a = str;
  150. while (len--)
  151. *a++ = c;
  152. return str;
  153. }
  154. #endif
  155. static __always_inline char * _dl_get_last_path_component(char *path)
  156. {
  157. register char *ptr = path-1;
  158. while (*++ptr)
  159. ;/* empty */
  160. /* strip trailing slashes */
  161. while (ptr != path && *--ptr == '/') {
  162. *ptr = '\0';
  163. }
  164. /* find last component */
  165. while (ptr != path && *--ptr != '/')
  166. ;/* empty */
  167. return ptr == path ? ptr : ptr+1;
  168. }
  169. #else /* IS_IN_rtld */
  170. # include <string.h>
  171. # define _dl_strlen strlen
  172. # define _dl_strcat strcat
  173. # define _dl_strcpy strcpy
  174. # define _dl_strcmp strcmp
  175. # define _dl_strchr strchr
  176. # define _dl_strrchr strrchr
  177. # define _dl_strstr strstr
  178. # define _dl_memcpy memcpy
  179. # define _dl_memcmp memcmp
  180. # define _dl_memset memset
  181. #endif /* IS_IN_rtld */
  182. #if defined IS_IN_rtld || defined __SUPPORT_LD_DEBUG__
  183. /* Early on, we can't call printf, so use this to print out
  184. * numbers using the SEND_STDERR() macro. Avoid using mod
  185. * or using long division */
  186. static __always_inline char * _dl_simple_ltoa(char *local, unsigned long i)
  187. {
  188. /* 20 digits plus a null terminator should be good for
  189. * 64-bit or smaller ints (2^64 - 1)*/
  190. char *p = &local[22];
  191. *--p = '\0';
  192. do {
  193. char temp;
  194. do_rem(temp, i, 10);
  195. *--p = '0' + temp;
  196. do_div_10(i, temp);
  197. } while (i > 0);
  198. return p;
  199. }
  200. #endif
  201. #ifdef IS_IN_rtld
  202. static __always_inline char * _dl_simple_ltoahex(char *local, unsigned long i)
  203. {
  204. /* 16 digits plus a leading "0x" plus a null terminator,
  205. * should be good for 64-bit or smaller ints */
  206. char *p = &local[22];
  207. *--p = '\0';
  208. do {
  209. char temp = i & 0xf;
  210. if (temp <= 0x09)
  211. *--p = '0' + temp;
  212. else
  213. *--p = 'a' - 0x0a + temp;
  214. i >>= 4;
  215. } while (i > 0);
  216. *--p = 'x';
  217. *--p = '0';
  218. return p;
  219. }
  220. /* The following macros may be used in dl-startup.c to debug
  221. * ldso before ldso has fixed itself up to make function calls */
  222. /* On some (wierd) arches, none of this stuff works at all, so
  223. * disable the whole lot... */
  224. /* The same applies for ARM FDPIC at least for the moment. */
  225. #if defined(__mips__) || defined(__FDPIC__)
  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 */