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
This commit is contained in:
Ben Gras
2013-05-31 15:42:01 +00:00
parent 49b9165251
commit e92490cf47
7 changed files with 512 additions and 53 deletions
+26 -34
View File
@@ -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;
}