From 0e9a2605200b721fdfcf32d2c95f79a81fc84352 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Wed, 24 Aug 2011 20:05:13 +0000 Subject: Rtld links with the specially built pic static libc library to get some C runtime services, like printf(). Unfortunately, the multithread-safeness measures in the libc do not work in rtld environment. Rip the kernel printf() implementation and use it in the rtld instead of libc version. This printf does not require any shared global data and thus is mt-safe. Systematically use rtld_printf() and related functions, remove the calls to err(3). Note that stdio is still pulled from libc due to libmap implementaion using fopen(). This is safe but unoptimal, and can be changed later. Reported and tested by: pgj Diagnosed and reviewed by: kan (previous version) Approved by: re (bz) --- libexec/rtld-elf/malloc.c | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) (limited to 'libexec/rtld-elf/malloc.c') diff --git a/libexec/rtld-elf/malloc.c b/libexec/rtld-elf/malloc.c index 38ae89af726e..95916598b53f 100644 --- a/libexec/rtld-elf/malloc.c +++ b/libexec/rtld-elf/malloc.c @@ -49,7 +49,6 @@ static char *rcsid = "$FreeBSD$"; #include #include -#include #include #include #include @@ -59,6 +58,7 @@ static char *rcsid = "$FreeBSD$"; #include #include #include +#include "rtld_printf.h" #ifndef BSD #define MAP_COPY MAP_PRIVATE #define MAP_FILE 0 @@ -150,8 +150,7 @@ botch(s) #endif /* Debugging stuff */ -static void xprintf(const char *, ...); -#define TRACE() xprintf("TRACE %s:%d\n", __FILE__, __LINE__) +#define TRACE() rtld_printf("TRACE %s:%d\n", __FILE__, __LINE__) extern int pagesize; @@ -503,7 +502,8 @@ int n; caddr_t addr = (caddr_t) (((long)pagepool_start + pagesz - 1) & ~(pagesz - 1)); if (munmap(addr, pagepool_end - addr) != 0) - warn("morepages: munmap %p", addr); + rtld_fdprintf(STDERR_FILENO, "morepages: munmap %p", + addr); } offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1)); @@ -511,7 +511,7 @@ int n; if ((pagepool_start = mmap(0, n * pagesz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_COPY, fd, 0)) == (caddr_t)-1) { - xprintf("Cannot map anonymous memory"); + rtld_printf("Cannot map anonymous memory\n"); return 0; } pagepool_end = pagepool_start + n * pagesz; @@ -522,18 +522,3 @@ int n; #endif return n; } - -/* - * Non-mallocing printf, for use by malloc itself. - */ -static void -xprintf(const char *fmt, ...) -{ - char buf[256]; - va_list ap; - - va_start(ap, fmt); - vsprintf(buf, fmt, ap); - (void)write(STDOUT_FILENO, buf, strlen(buf)); - va_end(ap); -} -- cgit v1.3