dl-string.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #ifndef _LINUX_STRING_H_
  2. #define _LINUX_STRING_H_
  3. #include <dl-sysdep.h> // for do_rem
  4. #include <features.h>
  5. static size_t _dl_strlen(const char * str);
  6. static char *_dl_strcat(char *dst, const char *src);
  7. static char * _dl_strcpy(char * dst,const char *src);
  8. static int _dl_strcmp(const char * s1,const char * s2);
  9. static int _dl_strncmp(const char * s1,const char * s2,size_t len);
  10. static char * _dl_strchr(const char * str,int c);
  11. static char *_dl_strrchr(const char *str, int c);
  12. static char *_dl_strstr(const char *s1, const char *s2);
  13. static void * _dl_memcpy(void * dst, const void * src, size_t len);
  14. static int _dl_memcmp(const void * s1,const void * s2,size_t len);
  15. static void *_dl_memset(void * str,int c,size_t len);
  16. static char *_dl_get_last_path_component(char *path);
  17. static char *_dl_simple_ltoa(char * local, unsigned long i);
  18. static char *_dl_simple_ltoahex(char * local, unsigned long i);
  19. #ifndef NULL
  20. #define NULL ((void *) 0)
  21. #endif
  22. static __always_inline size_t _dl_strlen(const char * str)
  23. {
  24. register const char *ptr = (char *) str-1;
  25. while (*++ptr);
  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. 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. }
  56. while (c1 == c2);
  57. return c1 - c2;
  58. }
  59. static __always_inline int _dl_strncmp(const char * s1,const char * s2,size_t len)
  60. {
  61. register unsigned char c1 = '\0';
  62. register unsigned char c2 = '\0';
  63. s1--;s2--;
  64. while (len > 0) {
  65. c1 = (unsigned char) *++s1;
  66. c2 = (unsigned char) *++s2;
  67. if (c1 == '\0' || c1 != c2)
  68. return c1 - c2;
  69. len--;
  70. }
  71. return c1 - c2;
  72. }
  73. static __always_inline char * _dl_strchr(const char * str,int c)
  74. {
  75. register char ch;
  76. str--;
  77. do {
  78. if ((ch = *++str) == c)
  79. return (char *) str;
  80. }
  81. while (ch);
  82. return 0;
  83. }
  84. static inline char * _dl_strrchr(const char *str, int c)
  85. {
  86. register char *prev = 0;
  87. register char *ptr = (char *) str-1;
  88. while (*++ptr != '\0') {
  89. if (*ptr == c)
  90. prev = ptr;
  91. }
  92. if (c == '\0')
  93. return(ptr);
  94. return(prev);
  95. }
  96. static inline char * _dl_strstr(const char *s1, const char *s2)
  97. {
  98. register const char *s = s1;
  99. register const char *p = s2;
  100. do {
  101. if (!*p) {
  102. return (char *) s1;;
  103. }
  104. if (*p == *s) {
  105. ++p;
  106. ++s;
  107. } else {
  108. p = s2;
  109. if (!*s) {
  110. return NULL;
  111. }
  112. s = ++s1;
  113. }
  114. } while (1);
  115. }
  116. static inline void * _dl_memcpy(void * dst, const void * src, size_t len)
  117. {
  118. register char *a = dst-1;
  119. register const char *b = src-1;
  120. while (len) {
  121. *++a = *++b;
  122. --len;
  123. }
  124. return dst;
  125. }
  126. static __always_inline int _dl_memcmp(const void * s1,const void * s2,size_t len)
  127. {
  128. unsigned char *c1 = (unsigned char *)s1-1;
  129. unsigned char *c2 = (unsigned char *)s2-1;
  130. while (len) {
  131. if (*++c1 != *++c2)
  132. return *c1 - *c2;
  133. len--;
  134. }
  135. return 0;
  136. }
  137. #if defined(powerpc)
  138. /* Will generate smaller and faster code due to loop unrolling.*/
  139. static __always_inline void * _dl_memset(void *to, int c, size_t n)
  140. {
  141. unsigned long chunks;
  142. unsigned long *tmp_to;
  143. unsigned char *tmp_char;
  144. chunks = n / 4;
  145. tmp_to = to + n;
  146. c = c << 8 | c;
  147. c = c << 16 | c;
  148. if (!chunks)
  149. goto lessthan4;
  150. do {
  151. *--tmp_to = c;
  152. } while (--chunks);
  153. lessthan4:
  154. n = n % 4;
  155. if (!n ) return to;
  156. tmp_char = (unsigned char *)tmp_to;
  157. do {
  158. *--tmp_char = c;
  159. } while (--n);
  160. return to;
  161. }
  162. #else
  163. static __always_inline void * _dl_memset(void * str,int c,size_t len)
  164. {
  165. register char *a = str;
  166. while (len--)
  167. *a++ = c;
  168. return str;
  169. }
  170. #endif
  171. static __always_inline char * _dl_get_last_path_component(char *path)
  172. {
  173. register char *ptr = path-1;
  174. while (*++ptr)
  175. ;/* empty */
  176. /* strip trailing slashes */
  177. while (ptr != path && *--ptr == '/') {
  178. *ptr = '\0';
  179. }
  180. /* find last component */
  181. while (ptr != path && *--ptr != '/')
  182. ;/* empty */
  183. return ptr == path ? ptr : ptr+1;
  184. }
  185. /* Early on, we can't call printf, so use this to print out
  186. * numbers using the SEND_STDERR() macro. Avoid using mod
  187. * or using long division */
  188. static __always_inline char * _dl_simple_ltoa(char * local, unsigned long i)
  189. {
  190. /* 20 digits plus a null terminator should be good for
  191. * 64-bit or smaller ints (2^64 - 1)*/
  192. char *p = &local[22];
  193. *--p = '\0';
  194. do {
  195. char temp;
  196. do_rem(temp, i, 10);
  197. *--p = '0' + temp;
  198. i /= 10;
  199. } while (i > 0);
  200. return p;
  201. }
  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(__mips__) \
  232. || defined(__sh__) || defined(__powerpc__)
  233. # define CONSTANT_STRING_GOT_FIXUP(X) \
  234. if ((X) < (const char *) load_addr) (X) += load_addr
  235. #else
  236. # define CONSTANT_STRING_GOT_FIXUP(X)
  237. #endif
  238. #define SEND_STDERR(X) \
  239. { \
  240. const char *tmp1 = (X); \
  241. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  242. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  243. }
  244. #define SEND_ADDRESS_STDERR(ADR, add_a_newline) \
  245. { \
  246. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  247. unsigned long X = (unsigned long)(ADR); \
  248. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  249. tmp2 = tmp1 + sizeof(tmp); \
  250. *--tmp2 = '\0'; \
  251. if (add_a_newline) *--tmp2 = '\n'; \
  252. do { \
  253. v = (X) & 0xf; \
  254. if (v <= 0x09) \
  255. *--tmp2 = '0' + v; \
  256. else \
  257. *--tmp2 = 'a' - 0x0a + v; \
  258. (X) >>= 4; \
  259. } while ((X) > 0); \
  260. *--tmp2 = 'x'; \
  261. *--tmp2 = '0'; \
  262. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  263. }
  264. #define SEND_NUMBER_STDERR(NUM, add_a_newline) \
  265. { \
  266. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  267. unsigned long X = (unsigned long)(NUM); \
  268. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  269. tmp2 = tmp1 + sizeof(tmp); \
  270. *--tmp2 = '\0'; \
  271. if (add_a_newline) *--tmp2 = '\n'; \
  272. do { \
  273. do_rem(v, (X), 10); \
  274. *--tmp2 = '0' + v; \
  275. (X) /= 10; \
  276. } while ((X) > 0); \
  277. _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  278. }
  279. #endif
  280. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  281. # define SEND_STDERR_DEBUG(X) SEND_STDERR(X)
  282. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline) SEND_NUMBER_STDERR(X, add_a_newline)
  283. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline) SEND_ADDRESS_STDERR(X, add_a_newline)
  284. #else
  285. # define SEND_STDERR_DEBUG(X)
  286. # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline)
  287. # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline)
  288. #endif
  289. #endif