dl-string.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #if defined(__mips__)
  225. # define SEND_STDERR(X)
  226. # define SEND_ADDRESS_STDERR(X, add_a_newline)
  227. # define SEND_NUMBER_STDERR(X, add_a_newline)
  228. #else
  229. /* On some arches constant strings are referenced through the GOT.
  230. * This requires that load_addr must already be defined... */
  231. #if defined(mc68000) || defined(__arm__) || defined(__thumb__) || \
  232. defined(__sh__) || defined(__powerpc__) || \
  233. defined(__avr32__) || defined(__xtensa__) || defined(__sparc__) || defined(__microblaze__)
  234. # define CONSTANT_STRING_GOT_FIXUP(X) \
  235. if ((X) < (const char *) load_addr) (X) += load_addr
  236. # define NO_EARLY_SEND_STDERR
  237. #else
  238. # define CONSTANT_STRING_GOT_FIXUP(X)
  239. #endif
  240. #define SEND_STDERR(X) \
  241. { \
  242. const char *tmp1 = (X); \
  243. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  244. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  245. }
  246. #define SEND_ADDRESS_STDERR(ADR, add_a_newline) \
  247. { \
  248. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  249. unsigned long X = (unsigned long)(ADR); \
  250. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  251. tmp2 = tmp1 + sizeof(tmp); \
  252. *--tmp2 = '\0'; \
  253. if (add_a_newline) *--tmp2 = '\n'; \
  254. do { \
  255. v = (X) & 0xf; \
  256. if (v <= 0x09) \
  257. *--tmp2 = '0' + v; \
  258. else \
  259. *--tmp2 = 'a' - 0x0a + v; \
  260. (X) >>= 4; \
  261. } while ((X) > 0); \
  262. *--tmp2 = 'x'; \
  263. *--tmp2 = '0'; \
  264. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  265. }
  266. #define SEND_NUMBER_STDERR(NUM, add_a_newline) \
  267. { \
  268. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  269. unsigned long X = (unsigned long)(NUM); \
  270. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  271. tmp2 = tmp1 + sizeof(tmp); \
  272. *--tmp2 = '\0'; \
  273. if (add_a_newline) *--tmp2 = '\n'; \
  274. do { \
  275. do_rem(v, (X), 10); \
  276. *--tmp2 = '0' + v; \
  277. do_div_10((X), v); \
  278. } while ((X) > 0); \
  279. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  280. }
  281. #endif
  282. /* Some targets may have to override this to something that doesn't
  283. * reference constant strings through the GOT. This macro should be
  284. * preferred over SEND_STDERR for constant strings before we complete
  285. * bootstrap.
  286. */
  287. #ifndef SEND_EARLY_STDERR
  288. # define SEND_EARLY_STDERR(S) SEND_STDERR(S)
  289. #else
  290. # define EARLY_STDERR_SPECIAL
  291. #endif
  292. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  293. # define SEND_STDERR_DEBUG(X) SEND_STDERR(X)
  294. # define SEND_EARLY_STDERR_DEBUG(X) SEND_EARLY_STDERR(X)
  295. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline) SEND_NUMBER_STDERR(X, add_a_newline)
  296. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline) SEND_ADDRESS_STDERR(X, add_a_newline)
  297. #else
  298. # define SEND_STDERR_DEBUG(X)
  299. # define SEND_EARLY_STDERR_DEBUG(X)
  300. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline)
  301. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline)
  302. #endif
  303. #endif /* IS_IN_rtld */
  304. #endif /* _DL_STRING_H */