From e92490cf47695ecdf5e50f4aa192f5b81d6ec8be Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Sat, 16 Mar 2013 03:19:47 +0000 Subject: [PATCH] ld.elf_so, grep, make: use mmap() . let the dynamic linker use a real file mmap(), reverting most of the minix-specific changes . except for a fallback mode for systems or filesystems that do not support mmap() on files . make, grep: let them use mmap() too, with fallback Change-Id: I11c0c9ad439d713917a6f5c2d196558f6c03eabd --- commands/grep/file.c | 2 - commands/grep/mmfile.c | 7 +-- libexec/ld.elf_so/Makefile | 2 +- libexec/ld.elf_so/map_object.c | 60 ++++++++++------------ .../{map_object.c => map_object_fallback.c} | 52 +------------------ libexec/ld.elf_so/rtld.h | 1 + usr.bin/make/parse.c | 23 ++++----- 7 files changed, 44 insertions(+), 103 deletions(-) copy libexec/ld.elf_so/{map_object.c => map_object_fallback.c} (91%) diff --git a/commands/grep/file.c b/commands/grep/file.c index 9a6ae8ebc..3982f075c 100644 --- a/commands/grep/file.c +++ b/commands/grep/file.c @@ -42,9 +42,7 @@ static size_t lnbuflen; #endif #define FILE_STDIO 0 -#ifndef __minix #define FILE_MMAP 1 -#endif #define FILE_GZIP 2 struct file { diff --git a/commands/grep/mmfile.c b/commands/grep/mmfile.c index 8cdfefe21..14438b42e 100644 --- a/commands/grep/mmfile.c +++ b/commands/grep/mmfile.c @@ -1,4 +1,3 @@ -#ifndef __minix /* $OpenBSD: mmfile.c,v 1.11 2006/09/19 05:52:23 otto Exp $ */ /*- @@ -40,6 +39,9 @@ #define MAX_MAP_LEN 1048576 +#define munmap minix_munmap +#define mmap minix_mmap + mmf_t * mmopen(char *fn, char *mode) { @@ -87,7 +89,7 @@ mmclose(mmf_t *mmf) char * mmfgetln(mmf_t *mmf, size_t *l) { - static char *p; + static unsigned char *p; char *start = mmf->ptr; /* Remove speed bump */ char *end = mmf->end; /* Remove speed bump */ @@ -101,4 +103,3 @@ mmfgetln(mmf_t *mmf, size_t *l) ++mmf->ptr; return p; } -#endif diff --git a/libexec/ld.elf_so/Makefile b/libexec/ld.elf_so/Makefile index 0ca15ffcd..d0b863276 100644 --- a/libexec/ld.elf_so/Makefile +++ b/libexec/ld.elf_so/Makefile @@ -68,7 +68,7 @@ CLIBOBJ!= cd ${NETBSDSRCDIR}/lib/libc && ${PRINTOBJDIR} SRCS+= rtld.c reloc.c symbol.c xmalloc.c xprintf.c debug.c \ map_object.c load.c search.c headers.c paths.c expand.c \ - tls.c symver.c diagassert.c + tls.c symver.c diagassert.c map_object_fallback.c .if ${USE_FORT} == "yes" .PATH.c: ${NETBSDSRCDIR}/lib/libc/misc diff --git a/libexec/ld.elf_so/map_object.c b/libexec/ld.elf_so/map_object.c index 027deeca3..fad5e4265 100644 --- a/libexec/ld.elf_so/map_object.c +++ b/libexec/ld.elf_so/map_object.c @@ -51,6 +51,10 @@ __RCSID("$NetBSD: map_object.c,v 1.45 2012/10/13 21:13:07 dholland Exp $"); #ifdef __minix #define munmap minix_munmap +#define mmap minix_mmap +#ifndef MAP_SHARED +#define MAP_SHARED MAP_PRIVATE /* minix: MAP_SHARED should be MAP_PRIVATE */ +#endif #endif #define MINIXVERBOSE 0 @@ -63,19 +67,6 @@ static int protflags(int); /* Elf flags -> mmap protection */ #define EA_UNDEF (~(Elf_Addr)0) -static void Pread(void *addr, size_t size, int fd, off_t off) -{ - int s; - if((s=pread(fd,addr, size, off)) < 0) { - _rtld_error("pread failed"); - exit(1); - } - -#if MINIXVERBOSE - fprintf(stderr, "read 0x%lx bytes from offset 0x%lx to addr 0x%lx\n", size, off, addr); -#endif -} - /* * Map a shared object into memory. The argument is a file descriptor, * which must be open on the object and positioned at its beginning. @@ -128,6 +119,7 @@ _rtld_map_object(const char *path, int fd, const struct stat *sb) caddr_t clear_addr; size_t nclear; #endif + size_t bsslen; if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) { _rtld_error("%s: not ELF file (too short)", path); @@ -142,21 +134,16 @@ _rtld_map_object(const char *path, int fd, const struct stat *sb) obj->ino = sb->st_ino; } -#ifdef __minix - ehdr = minix_mmap(NULL, _rtld_pagesz, PROT_READ|PROT_WRITE, - MAP_PREALLOC|MAP_ANON, -1, (off_t)0); - Pread(ehdr, _rtld_pagesz, fd, 0); -#if MINIXVERBOSE - fprintf(stderr, "minix mmap for header: 0x%lx\n", ehdr); -#endif -#else ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd, (off_t)0); -#endif obj->ehdr = ehdr; if (ehdr == MAP_FAILED) { +#if defined(__minix) && defined(RTLD_LOADER) + return _rtld_map_object_fallback(path, fd, sb); +#else _rtld_error("%s: read error: %s", path, xstrerror(errno)); goto bad; +#endif } /* Make sure the file is valid */ if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0) { @@ -346,6 +333,8 @@ _rtld_map_object(const char *path, int fd, const struct stat *sb) /* Unmap header if it overlaps the first load section. */ if (base_offset < _rtld_pagesz) { + dbg(("error; base_offset = 0x%lx, pagesize 0x%lx\n", + base_offset, _rtld_pagesz)); munmap(ehdr, _rtld_pagesz); obj->ehdr = MAP_FAILED; } @@ -370,17 +359,9 @@ _rtld_map_object(const char *path, int fd, const struct stat *sb) #endif mapsize = base_vlimit - base_vaddr; -#ifndef __minix mapbase = mmap(base_addr, mapsize, text_flags, mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset); -#else - mapbase = minix_mmap(base_addr, mapsize, PROT_READ|PROT_WRITE, - MAP_ANON | MAP_PREALLOC, -1, 0); -#if MINIXVERBOSE - fprintf(stderr, "minix mmap for whole block: 0x%lx-0x%lx\n", mapbase, mapbase+mapsize); -#endif - Pread(mapbase, obj->textsize, fd, 0); -#endif + if (mapbase == MAP_FAILED) { _rtld_error("mmap of entire address space failed: %s", xstrerror(errno)); @@ -389,9 +370,7 @@ _rtld_map_object(const char *path, int fd, const struct stat *sb) /* Overlay the data segment onto the proper region. */ data_addr = mapbase + (data_vaddr - base_vaddr); -#ifdef __minix - Pread(data_addr, data_vlimit - data_vaddr, fd, data_offset); -#else + if (mmap(data_addr, data_vlimit - data_vaddr, data_flags, MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) == MAP_FAILED) { @@ -400,7 +379,13 @@ _rtld_map_object(const char *path, int fd, const struct stat *sb) } /* Overlay the bss segment onto the proper region. */ +#ifdef __minix + bsslen = base_vlimit - data_vlimit; + if (bsslen > 0 && + mmap(mapbase + data_vlimit - base_vaddr, bsslen, +#else if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit, +#endif data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) == MAP_FAILED) { _rtld_error("mmap of bss failed: %s", xstrerror(errno)); @@ -408,6 +393,7 @@ _rtld_map_object(const char *path, int fd, const struct stat *sb) } /* Unmap the gap between the text and data. */ +#ifndef __minix gap_addr = mapbase + round_up(text_vlimit - base_vaddr); gap_size = data_addr - gap_addr; if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) { @@ -514,5 +500,11 @@ protflags(int elfflags) #endif if (elfflags & PF_X) prot |= PROT_EXEC; +#ifdef __minix + /* Minix has to map it writable so we can do relocations + * as we don't have mprotect() yet. + */ + prot |= PROT_WRITE; +#endif return prot; } diff --git a/libexec/ld.elf_so/map_object.c b/libexec/ld.elf_so/map_object_fallback.c similarity index 91% copy from libexec/ld.elf_so/map_object.c copy to libexec/ld.elf_so/map_object_fallback.c index 027deeca3..49b72860e 100644 --- a/libexec/ld.elf_so/map_object.c +++ b/libexec/ld.elf_so/map_object_fallback.c @@ -76,15 +76,9 @@ static void Pread(void *addr, size_t size, int fd, off_t off) #endif } -/* - * Map a shared object into memory. The argument is a file descriptor, - * which must be open on the object and positioned at its beginning. - * - * The return value is a pointer to a newly-allocated Obj_Entry structure - * for the shared object. Returns NULL on failure. - */ +/* minix-without-mmap version of _rtld_map_object() */ Obj_Entry * -_rtld_map_object(const char *path, int fd, const struct stat *sb) +_rtld_map_object_fallback(const char *path, int fd, const struct stat *sb) { Obj_Entry *obj; Elf_Ehdr *ehdr; @@ -455,48 +449,6 @@ bad: return NULL; } -void -_rtld_obj_free(Obj_Entry *obj) -{ - Objlist_Entry *elm; - -#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) - if (obj->tls_done) - _rtld_tls_offset_free(obj); -#endif - xfree(obj->path); - while (obj->needed != NULL) { - Needed_Entry *needed = obj->needed; - obj->needed = needed->next; - xfree(needed); - } - while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) { - SIMPLEQ_REMOVE_HEAD(&obj->dldags, link); - xfree(elm); - } - while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) { - SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link); - xfree(elm); - } - if (!obj->phdr_loaded) - xfree((void *)(uintptr_t)obj->phdr); - xfree(obj); -#ifdef COMBRELOC - _rtld_combreloc_reset(obj); -#endif -} - -Obj_Entry * -_rtld_obj_new(void) -{ - Obj_Entry *obj; - - obj = CNEW(Obj_Entry); - SIMPLEQ_INIT(&obj->dldags); - SIMPLEQ_INIT(&obj->dagmembers); - return obj; -} - /* * Given a set of ELF protection flags, return the corresponding protection * flags for MMAP. diff --git a/libexec/ld.elf_so/rtld.h b/libexec/ld.elf_so/rtld.h index 0d3253dc9..77d2ef010 100644 --- a/libexec/ld.elf_so/rtld.h +++ b/libexec/ld.elf_so/rtld.h @@ -451,6 +451,7 @@ __dso_public extern void *___tls_get_addr(void *) /* map_object.c */ struct stat; Obj_Entry *_rtld_map_object(const char *, int, const struct stat *); +Obj_Entry *_rtld_map_object_fallback(const char *, int, const struct stat *); void _rtld_obj_free(Obj_Entry *); Obj_Entry *_rtld_obj_new(void); diff --git a/usr.bin/make/parse.c b/usr.bin/make/parse.c index d6dc1e85f..3bc751e6d 100644 --- a/usr.bin/make/parse.c +++ b/usr.bin/make/parse.c @@ -68,6 +68,15 @@ * SUCH DAMAGE. */ + +#ifdef __minix +#define mmap minix_mmap +#define munmap minix_munmap +#ifndef MAP_COPY +#define MAP_COPY MAP_PRIVATE +#endif +#endif + #ifndef MAKE_NATIVE static char rcsid[] = "$NetBSD: parse.c,v 1.185 2012/06/12 19:21:51 joerg Exp $"; #else @@ -133,9 +142,6 @@ __RCSID("$NetBSD: parse.c,v 1.185 2012/06/12 19:21:51 joerg Exp $"); #include #include -#ifndef MAP_FILE -#define MAP_FILE 0 -#endif #ifndef MAP_COPY #define MAP_COPY MAP_PRIVATE #endif @@ -403,9 +409,7 @@ loadedfile_destroy(struct loadedfile *lf) { if (lf->buf != NULL) { if (lf->maplen > 0) { -#ifndef __minix munmap(lf->buf, lf->maplen); -#endif } else { free(lf->buf); } @@ -430,7 +434,6 @@ loadedfile_nextbuf(void *x, size_t *len) return lf->buf; } -#ifndef __minix /* * Try to get the size of a file. */ @@ -463,7 +466,6 @@ load_getsize(int fd, size_t *ret) *ret = (size_t) st.st_size; return SUCCESS; } -#endif /* ndef __minix */ /* * Read in a file. @@ -479,9 +481,7 @@ static struct loadedfile * loadfile(const char *path, int fd) { struct loadedfile *lf; -#ifndef __minix long pagesize; -#endif ssize_t result; size_t bufpos; @@ -501,7 +501,6 @@ loadfile(const char *path, int fd) #endif } -#ifndef __minix if (load_getsize(fd, &lf->len) == SUCCESS) { /* found a size, try mmap */ pagesize = sysconf(_SC_PAGESIZE); @@ -524,6 +523,7 @@ loadfile(const char *path, int fd) * FUTURE: remove PROT_WRITE when the parser no longer * needs to scribble on the input. */ + lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE, MAP_FILE|MAP_COPY, fd, 0); if (lf->buf != MAP_FAILED) { @@ -539,7 +539,6 @@ loadfile(const char *path, int fd) goto done; } } -#endif /* cannot mmap; load the traditional way */ @@ -572,9 +571,7 @@ loadfile(const char *path, int fd) lf->buf = bmake_realloc(lf->buf, lf->len); } -#ifndef __minix done: -#endif /* !defined(__minix) */ if (path != NULL) { close(fd); } -- 2.11.4.GIT