mailing list of musl libc
 help / color / mirror / code / Atom feed
b22082e21dd8fba284a3ee5df7170878fbef2f72 blob 9533 bytes (raw)

  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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
 
#define SYSCALL_NO_TLS

#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <features.h>
#include <libgen.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
#include "atomic.h"
#include "dynlink.h"
#include "syscall.h"

extern weak hidden const size_t _DYNAMIC[];

int main();
weak void _init();
weak void _fini();
weak _Noreturn int __libc_start_main(int (*)(), int, char **,
	void (*)(), void(*)(), void(*)());

#define START "_start"
#define _dlstart_c _start_c
#define DL_DNI
#include "../ldso/dlstart.c"

struct dso {
	unsigned char *base;
	struct fdpic_loadmap *loadmap;
	size_t *dynv;
	Phdr *phdr;
	int phnum;
	size_t phentsize;
	unsigned char *map;
	size_t map_len;
};

static size_t page_size;

#ifndef PAGE_SIZE
#define PAGE_SIZE page_size
#endif

#ifdef SYS_mmap2
#define mmap(start, len, prot, flags, fd, off) (void*)__syscall(SYS_mmap2, start, len, prot, flags, fd, off/SYSCALL_MMAP2_UNIT)
#else
#define mmap(start, len, prot, flags, fd, off) (void*)__syscall(SYS_mmap, start, len, prot, flags, fd, off)
#endif

#define munmap(ptr, len) __syscall(SYS_munmap, ptr, len)

static inline int crt_mprotect(void *addr, size_t len, int prot)
{
	size_t start, end;
	start = (size_t)addr & -PAGE_SIZE;
	end = (size_t)((char *)addr + len + PAGE_SIZE-1) & -PAGE_SIZE;
	return __syscall(SYS_mprotect, start, end-start, prot);
}
#define mprotect crt_mprotect

#define read(fd, buf, size) __syscall(SYS_read, fd, buf, size)
#define pread(fd, buf, size, ofs) __syscall(SYS_pread, fd, buf, size, __SYSCALL_LL_PRW(ofs))

static inline off_t crt_lseek(int fd, off_t offset, int whence)
{
#ifdef SYS__llseek
	off_t result;
	return __syscall(SYS__llseek, fd, offset>>32, offset, &result, whence) ? -1 : result;
#else
	return __syscall(SYS_lseek, fd, offset, whence);
#endif
}
#define lseek crt_lseek

static inline void *map_library_allocz(size_t *size)
{
	void *ret;
	*size = (*size + SYSCALL_MMAP2_UNIT - 1) & -SYSCALL_MMAP2_UNIT;
	ret = mmap(0, *size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (ret == MAP_FAILED)
		return NULL;
	return ret;
}

static inline void map_library_free(void *ptr, size_t size)
{
	munmap(ptr, size);
}

#define map_library_failed(val) ((unsigned long)val > -4096UL)
#define map_library_error(ret) (-(long)ret)

#ifdef SYS_readlink
#define readlink(path, buf, bufsize) __syscall(SYS_readlink, path, buf, bufsize)
#else
#define readlink(path, buf, bufsize) __syscall(SYS_readlinkat, AT_FDCWD, path, buf, bufsize)
#endif

#ifdef SYS_access
#define access(filename, amode) __syscall(SYS_access, filename, amode)
#else
#define access(filename, amode) __syscall(SYS_faccessat, AT_FDCWD, filename, amode, 0)
#endif

static void *crt_memcpy(void *restrict dest, const void *restrict src, size_t n)
{
	unsigned char *d = dest;
	const unsigned char *s = src;
	for (; n; n--) *d++ = *s++;
	return dest;
}

static void *crt_memset(void *dest, int c, size_t n)
{
	unsigned char *s = dest;
	for (; n; n--, s++) *s = c;
	return dest;
}
#define memset crt_memset

static size_t crt_strlen(const char *s)
{
	const char *a = s;
	for (; *s; s++);
	return s-a;
}

static char *crt_strchrnul(const char *s, int c)
{
	c = (unsigned char)c;
	if (!c) return (char *)s + crt_strlen(s);
	for (; *s && *(unsigned char *)s != c; s++);
	return (char *)s;
}

static int crt_strncmp(const char *_l, const char *_r, size_t n)
{
	const unsigned char *l=(void *)_l, *r=(void *)_r;
	if (!n--) return 0;
	for (; *l && *r && n && *l == *r ; l++, r++, n--);
	return *l - *r;
}

static char *crt_getenv(const char *name, char **environ)
{
	size_t l = crt_strchrnul(name, '=') - name;
	if (l && !name[l] && environ)
		for (char **e = environ; *e; e++)
			if (!crt_strncmp(name, *e, l) && l[*e] == '=')
				return *e + l+1;
	return 0;
}

#define LD_CRT
#include "../ldso/map_library.h"

static void decode_vec(const size_t *v, size_t *a, size_t cnt)
{
	size_t i;
	for (i=0; i<cnt; i++) a[i] = 0;
	for (; v[0]; v+=2) if (v[0]-1<cnt-1) {
		a[0] |= 1UL<<v[0];
		a[v[0]] = v[1];
	}
}

static void get_rpath(const char **runpath, const size_t *dyn, unsigned char *base)
{
	/* DT_STRTAB is pre-relocated for us by dlstart */
	const char *strings = (char*)base + dyn[DT_STRTAB];

	if (dyn[0] & (1 << DT_RPATH))
		*runpath = strings + dyn[DT_RPATH];
	if (dyn[0] & (1 << DT_RUNPATH))
		*runpath = strings + dyn[DT_RUNPATH];
}

static size_t find_linker(char *outbuf, size_t bufsize, const char *this_path, size_t thisl, const size_t *dyn, unsigned char *base, char **environ, int secure)
{
	const char *paths[3] = {0}; // rpath, envpath, runpath
	size_t i;
	int fd;

	// In the suid/secure case, skip everything and use the fixed path
	if (secure)
		goto default_path;

	// Strip filename
	if (thisl)
		thisl--;
	while (thisl > 1 && this_path[thisl] == '/')
		thisl--;
	while (thisl > 0 && this_path[thisl] != '/')
		thisl--;

	const char *envpath = crt_getenv("LD_LOADER_PATH", environ);
	if (envpath) {
		size_t envlen = crt_strlen(envpath);
		if (envlen < bufsize) {
			crt_memcpy(outbuf, envpath, envlen + 1);
			return envlen + 1;
		}
	}

	get_rpath(&paths[1], dyn, base);

	paths[0] = crt_getenv("LD_LIBRARY_PATH", environ);

	for (i = 0; i < 2; i++) {
		const char *p = paths[i];
		char *o = outbuf;
		if (!p)
			continue;
		for (;;) {
			if (!crt_strncmp(p, "$ORIGIN", 7) ||
					!crt_strncmp(p, "${ORIGIN}", 9)) {
				if (o + thisl + 1 < outbuf + bufsize) {
					crt_memcpy(o, this_path, thisl);
					o += thisl;
				} else {
					o = outbuf + bufsize - 1;
				}
				p += (p[1] == '{' ? 9 : 7);
			} else if (*p == ':' || !*p) {
#define LDSO_FILENAME "ld-musl-" LDSO_ARCH ".so.1"
				if (o + sizeof(LDSO_FILENAME) + 1 < outbuf + bufsize) {
					*o++ = '/';
					crt_memcpy(o, LDSO_FILENAME, sizeof(LDSO_FILENAME));
					if (!access(outbuf, R_OK | X_OK))
						return (o + sizeof(LDSO_FILENAME)) - outbuf;
				}
				if (!*p)
					break;
				o = outbuf;
				p++;
			} else {
				if (o < outbuf + bufsize)
					*o++ = *p;
				p++;
			}
		}
	}

	default_path:
	// Didn't find a usable loader anywhere (or in secure mode), so try the default
	crt_memcpy(outbuf, LDSO_PATHNAME, sizeof(LDSO_PATHNAME));
	return sizeof(LDSO_PATHNAME);
}

hidden _Noreturn void __dls2(unsigned char *base, size_t *p)
{
	int argc = p[0];
	char **argv = (void *)(p+1);
	int fd;
	int secure;
	int prot = PROT_READ;
	struct dso loader;
	Ehdr *loader_hdr;
	Phdr *new_hdr;
	void *entry;
	char this_path[PATH_MAX] = {0};
	size_t thisl;
	char linker_path[PATH_MAX] = {0};
	size_t linker_len;
	size_t i;
	size_t aux[AUX_CNT];
	size_t *auxv;
	size_t dyn[DYN_CNT];
	char **environ = argv + argc + 1;

	// We're already finished here; just run main.
	if (__libc_start_main)
		__libc_start_main(main, argc, argv, _init, _fini, 0);

	/* Find aux vector just past environ[] and use it to initialize
	* global data that may be needed before we can make syscalls. */
	for (i = argc + 1; argv[i]; i++);
	auxv = (void *)(argv + i + 1);
	decode_vec(auxv, aux, AUX_CNT);
	secure = ((aux[0] & 0x7800) != 0x7800 || aux[AT_UID] != aux[AT_EUID]
		|| aux[AT_GID] != aux[AT_EGID] || aux[AT_SECURE]);

	page_size = aux[AT_PAGESZ];

	decode_vec(_DYNAMIC, dyn, DYN_CNT);

	thisl = readlink("/proc/self/exe", this_path, sizeof this_path);
	linker_len = find_linker(linker_path, sizeof linker_path, this_path, thisl, dyn, base, environ, secure);

	fd = __sys_open2(, linker_path, O_RDONLY);
	if (fd < 0)
		goto error;

	loader_hdr = map_library(fd, &loader);
	if (!loader_hdr)
		goto error;

	__syscall(SYS_close, fd);

	// Copy the program headers into an anonymous mapping
	new_hdr = mmap(0, (aux[AT_PHENT] * (aux[AT_PHNUM] + 2) + linker_len + PAGE_SIZE - 1) & -PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (map_library_failed(new_hdr))
		goto error;

	// Point it back at the original kernel-provided base
	new_hdr->p_type = PT_PHDR;
	new_hdr->p_vaddr = (size_t)new_hdr - (size_t)base;

	((Phdr*)((char*)new_hdr + aux[AT_PHENT]))->p_type = PT_INTERP;
	((Phdr*)((char*)new_hdr + aux[AT_PHENT]))->p_vaddr = new_hdr->p_vaddr + aux[AT_PHENT] * (aux[AT_PHNUM] + 2);

	crt_memcpy((char*)new_hdr + aux[AT_PHENT] * (aux[AT_PHNUM] + 2), linker_path, linker_len);

	for (i = 0; i < aux[AT_PHNUM]; i++) {
		Phdr *hdr = (void*)((char*)aux[AT_PHDR] + aux[AT_PHENT] * i);
		Phdr *dst = (void*)((char*)new_hdr + aux[AT_PHENT] * (i + 2));
		if (hdr->p_type == PT_PHDR || hdr->p_type == PT_INTERP) {
			// Can't have a duplicate
			dst->p_type = PT_NULL;
		} else {
			crt_memcpy(dst, hdr, aux[AT_PHENT]);
		}
	}

	if (mprotect(new_hdr, aux[AT_PHENT] * (aux[AT_PHNUM] + 2) + linker_len, PROT_READ))
		goto error;

	for (i=0; auxv[i]; i+=2) {
		if (auxv[i] == AT_BASE)
			auxv[i + 1] = (size_t)loader_hdr;
		if (auxv[i] == AT_PHDR)
			auxv[i + 1] = (size_t)new_hdr;
		if (auxv[i] == AT_PHNUM)
			auxv[i + 1] += 2;
	}

	entry = laddr(&loader, loader_hdr->e_entry);

#ifndef LD_FDPIC
	/* Undo the relocations performed by dlstart */

	if (NEED_MIPS_GOT_RELOCS) {
		const size_t *dynv = _DYNAMIC;
		size_t local_cnt = 0;
		size_t *got = (void *)(base + dyn[DT_PLTGOT]);
		for (i=0; dynv[i]; i+=2) if (dynv[i]==DT_MIPS_LOCAL_GOTNO)
			local_cnt = dynv[i+1];
		for (i=0; i<local_cnt; i++) got[i] -= (size_t)base;
	}

	size_t *rel = (void *)((size_t)base+dyn[DT_REL]);
	size_t rel_size = dyn[DT_RELSZ];
	for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) {
		if (!IS_RELATIVE(rel[1], 0)) continue;
		size_t *rel_addr = (void *)((size_t)base + rel[0]);
		*rel_addr -= (size_t)base;
	}
#endif

	CRTJMP(entry, argv - 1);

error:
	for(;;) a_crash();
}
debug log:

solving b22082e ...
found b22082e in https://inbox.vuxu.org/musl/1568091399-23967-3-git-send-email-rodger.combs@gmail.com/

applying [1/1] https://inbox.vuxu.org/musl/1568091399-23967-3-git-send-email-rodger.combs@gmail.com/
diff --git a/crt/dcrt1.c b/crt/dcrt1.c
new file mode 100644
index 0000000..b22082e

Checking patch crt/dcrt1.c...
Applied patch crt/dcrt1.c cleanly.

index at:
100644 b22082e21dd8fba284a3ee5df7170878fbef2f72	crt/dcrt1.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).