summaryrefslogtreecommitdiff
path: root/src/ldso/dynlink.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2015-06-23 04:03:42 +0000
committerRich Felker <dalias@aerifal.cx>2015-06-23 04:05:31 +0000
commitce337daa00e42d4f2d9a4d9ae0ed51b20249d924 (patch)
treed09096368d1987a8ea306fb279a24c7e91bf3356 /src/ldso/dynlink.c
parenta59341420fdedb288d9ff80e73609ae44e9cf258 (diff)
downloadmusl-ce337daa00e42d4f2d9a4d9ae0ed51b20249d924.tar.gz
musl-ce337daa00e42d4f2d9a4d9ae0ed51b20249d924.tar.bz2
musl-ce337daa00e42d4f2d9a4d9ae0ed51b20249d924.tar.xz
musl-ce337daa00e42d4f2d9a4d9ae0ed51b20249d924.zip
make dynamic linker work around MAP_FAILED mmap failure on nommu kernels
previously, loading of additional libraries beyond libc/ldso did not work on nommu kernels, nor did loading programs via invocation of the dynamic linker as a command.
Diffstat (limited to 'src/ldso/dynlink.c')
-rw-r--r--src/ldso/dynlink.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c
index 7e566933..b77c6f6b 100644
--- a/src/ldso/dynlink.c
+++ b/src/ldso/dynlink.c
@@ -423,6 +423,28 @@ static void reclaim_gaps(struct dso *dso)
}
}
+static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
+{
+ char *q = mmap(p, n, prot, flags, fd, off);
+ if (q != MAP_FAILED || errno != EINVAL) return q;
+ /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
+ if (flags & MAP_ANONYMOUS) {
+ memset(p, 0, n);
+ return p;
+ }
+ ssize_t r;
+ if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
+ for (q=p; n; q+=r, off+=r, n-=r) {
+ r = read(fd, q, n);
+ if (r < 0 && errno != EINTR) return MAP_FAILED;
+ if (!r) {
+ memset(q, 0, n);
+ break;
+ }
+ }
+ return p;
+}
+
static void *map_library(int fd, struct dso *dso)
{
Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
@@ -524,13 +546,13 @@ static void *map_library(int fd, struct dso *dso)
prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
((ph->p_flags&PF_X) ? PROT_EXEC : 0));
- if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
+ if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
goto error;
if (ph->p_memsz > ph->p_filesz) {
size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
- if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
+ if (pgbrk-(size_t)base < this_max && mmap_fixed((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
goto error;
}
}