summaryrefslogtreecommitdiff
path: root/ldso/include/dl-string.h
blob: 14ae617c3784ff5da60a6a9cb012745b33f59e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* vi: set sw=4 ts=4: */
/*
 * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
 *
 * GNU Lesser General Public License version 2.1 or later.
 */

#ifndef _DL_STRING_H
#define _DL_STRING_H

#include <features.h>

#define __need_NULL
#include <stddef.h>

#include <dl-defs.h> /* for do_rem by dl-sysdep.h */

/* provide some sane defaults */
#ifndef do_rem
# define do_rem(result, n, base) ((result) = (n) % (base))
#endif
#ifndef do_div_10
# define do_div_10(result, remain) ((result) /= 10)
#endif

#ifdef IS_IN_rtld
static __always_inline size_t _dl_strlen(const char *str)
{
	register const char *ptr = (char *) str-1;
	while (*++ptr)
		;/* empty */
	return (ptr - str);
}

static __always_inline char * _dl_strcat(char *dst, const char *src)
{
	register char *ptr = dst-1;

	src--;
	while (*++ptr)
		;/* empty */
	ptr--;
	while ((*++ptr = *++src) != 0)
		;/* empty */
	return dst;
}

static __always_inline char * _dl_strcpy(char *dst, const char *src)
{
	register char *ptr = dst;

	dst--;src--;
	while ((*++dst = *++src) != 0)
		;/* empty */

	return ptr;
}

static __always_inline int _dl_strcmp(const char *s1, const char *s2)
{
	register unsigned char c1, c2;
	s1--;s2--;
	do {
		c1 = (unsigned char) *++s1;
		c2 = (unsigned char) *++s2;
		if (c1 == '\0')
			return c1 - c2;
	} while (c1 == c2);

	return c1 - c2;
}

static __always_inline char * _dl_strchr(const char *str, int c)
{
	register char ch;
	str--;
	do {
		if ((ch = *++str) == c)
			return (char *) str;
	}
	while (ch);

	return 0;
}

static __always_inline char * _dl_strrchr(const char *str, int c)
{
	register char *prev = 0;
	register char *ptr = (char *) str-1;

	while (*++ptr != '\0') {
		if (*ptr == c)
			prev = ptr;
	}
	if (c == '\0')
		return(ptr);
	return(prev);
}

static __always_inline char * _dl_strstr(const char *s1, const char *s2)
{
	register const char *s = s1;
	register const char *p = s2;

	do {
		if (!*p)
			return (char *) s1;;
		if (*p == *s) {
			++p;
			++s;
		} else {
			p = s2;
			if (!*s)
				return NULL;
			s = ++s1;
		}
	} while (1);
}

static __always_inline void * _dl_memcpy(void *dst, const void *src, size_t len)
{
	register char *a = dst-1;
	register const char *b = src-1;

	while (len) {
		*++a = *++b;
		--len;
	}
	return dst;
}

static __always_inline int _dl_memcmp(const void *s1, const void *s2, size_t len)
{
	unsigned char *c1 = (unsigned char *)s1-1;
	unsigned char *c2 = (unsigned char *)s2-1;

	while (len) {
		if (*++c1 != *++c2)
			return *c1 - *c2;
		len--;
	}
	return 0;
}

#if defined(__powerpc__)
/* Will generate smaller and faster code due to loop unrolling.*/
static __always_inline void * _dl_memset(void *to, int c, size_t n)
{
	unsigned long chunks;
	unsigned long *tmp_to;
	unsigned char *tmp_char;

	chunks = n / 4;
	tmp_to = to + n;
	c = c << 8 | c;
	c = c << 16 | c;
	if (!chunks)
		goto lessthan4;
	do {
		*--tmp_to = c;
	} while (--chunks);
lessthan4:
	n = n % 4;
	if (!n)
		return to;
	tmp_char = (unsigned char *)tmp_to;
	do {
		*--tmp_char = c;
	} while (--n);
	return to;
}
#else
static __always_inline void * _dl_memset(void *str, int c, size_t len)
{
	register char *a = str;

	while (len--)
		*a++ = c;

	return str;
}
#endif

static __always_inline char * _dl_get_last_path_component(char *path)
{
	register char *ptr = path-1;

	while (*++ptr)
		;/* empty */

	/* strip trailing slashes */
	while (ptr != path && *--ptr == '/') {
		*ptr = '\0';
	}

	/* find last component */
	while (ptr != path && *--ptr != '/')
		;/* empty */
	return ptr == path ? ptr : ptr+1;
}
#else /* IS_IN_rtld */
# include <string.h>
# define _dl_strlen strlen
# define _dl_strcat strcat
# define _dl_strcpy strcpy
# define _dl_strcmp strcmp
# define _dl_strchr strchr
# define _dl_strrchr strrchr
# define _dl_strstr strstr
# define _dl_memcpy memcpy
# define _dl_memcmp memcmp
# define _dl_memset memset
#endif /* IS_IN_rtld */

#if defined IS_IN_rtld || defined __SUPPORT_LD_DEBUG__
/* Early on, we can't call printf, so use this to print out
 * numbers using the SEND_STDERR() macro.  Avoid using mod
 * or using long division */
static __always_inline char * _dl_simple_ltoa(char *local, unsigned long i)
{
	/* 20 digits plus a null terminator should be good for
	 * 64-bit or smaller ints (2^64 - 1)*/
	char *p = &local[22];
	*--p = '\0';
	do {
		char temp;
		do_rem(temp, i, 10);
		*--p = '0' + temp;
		do_div_10(i, temp);
	} while (i > 0);
	return p;
}
#endif

#ifdef IS_IN_rtld
static __always_inline char * _dl_simple_ltoahex(char *local, unsigned long i)
{
	/* 16 digits plus a leading "0x" plus a null terminator,
	 * should be good for 64-bit or smaller ints */
	char *p = &local[22];
	*--p = '\0';
	do {
		char temp = i & 0xf;
		if (temp <= 0x09)
			*--p = '0' + temp;
		else
			*--p = 'a' - 0x0a + temp;
		i >>= 4;
	} while (i > 0);
	*--p = 'x';
	*--p = '0';
	return p;
}

/* The following macros may be used in dl-startup.c to debug
 * ldso before ldso has fixed itself up to make function calls */

/* On some (wierd) arches, none of this stuff works at all, so
 * disable the whole lot... */
#if defined(__mips__)

# define SEND_STDERR(X)
# define SEND_ADDRESS_STDERR(X, add_a_newline)
# define SEND_NUMBER_STDERR(X, add_a_newline)

#else

/* On some arches constant strings are referenced through the GOT.
 * This requires that load_addr must already be defined... */
#if defined(mc68000)  || defined(__arm__) || defined(__thumb__) || \
    defined(__sh__) || defined(__powerpc__) || \
    defined(__avr32__) || defined(__xtensa__) || defined(__sparc__) || defined(__microblaze__)
# define CONSTANT_STRING_GOT_FIXUP(X) \
	if ((X) < (const char *) load_addr) (X) += load_addr
# define NO_EARLY_SEND_STDERR
#else
# define CONSTANT_STRING_GOT_FIXUP(X)
#endif

#define SEND_STDERR(X) \
{ \
	const char *tmp1 = (X); \
	CONSTANT_STRING_GOT_FIXUP(tmp1); \
	_dl_write(2, tmp1, _dl_strlen(tmp1)); \
}

#define SEND_ADDRESS_STDERR(ADR, add_a_newline) \
{ \
	char tmp[26], v, *tmp2, *tmp1 = tmp; \
	unsigned long X = (unsigned long)(ADR); \
	CONSTANT_STRING_GOT_FIXUP(tmp1); \
	tmp2 = tmp1 + sizeof(tmp); \
	*--tmp2 = '\0'; \
	if (add_a_newline) *--tmp2 = '\n'; \
	do { \
		v = (X) & 0xf; \
		if (v <= 0x09) \
			*--tmp2 = '0' + v; \
		else \
			*--tmp2 = 'a' - 0x0a + v; \
		(X) >>= 4; \
	} while ((X) > 0); \
	*--tmp2 = 'x'; \
	*--tmp2 = '0'; \
	_dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
}

#define SEND_NUMBER_STDERR(NUM, add_a_newline) \
{ \
	char tmp[26], v, *tmp2, *tmp1 = tmp; \
	unsigned long X = (unsigned long)(NUM); \
	CONSTANT_STRING_GOT_FIXUP(tmp1); \
	tmp2 = tmp1 + sizeof(tmp); \
	*--tmp2 = '\0'; \
	if (add_a_newline) *--tmp2 = '\n'; \
	do { \
		do_rem(v, (X), 10); \
		*--tmp2 = '0' + v; \
		do_div_10((X), v); \
	} while ((X) > 0); \
	_dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
}
#endif

/* Some targets may have to override this to something that doesn't
 * reference constant strings through the GOT.  This macro should be
 * preferred over SEND_STDERR for constant strings before we complete
 * bootstrap.
 */
#ifndef SEND_EARLY_STDERR
# define SEND_EARLY_STDERR(S) SEND_STDERR(S)
#else
# define EARLY_STDERR_SPECIAL
#endif

#ifdef __SUPPORT_LD_DEBUG_EARLY__
# define SEND_STDERR_DEBUG(X) SEND_STDERR(X)
# define SEND_EARLY_STDERR_DEBUG(X) SEND_EARLY_STDERR(X)
# define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline) SEND_NUMBER_STDERR(X, add_a_newline)
# define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline) SEND_ADDRESS_STDERR(X, add_a_newline)
#else
# define SEND_STDERR_DEBUG(X)
# define SEND_EARLY_STDERR_DEBUG(X)
# define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline)
# define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline)
#endif

#endif /* IS_IN_rtld */

#endif /* _DL_STRING_H */